blob: 6eb58a8ed03c6c2158affb5249f37aeabe49f741 [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>
Nicolai Stange49d200d2016-03-22 14:11:14 +010026#include <asm/poll.h>
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010027
28#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Nicolai Stange49d200d2016-03-22 14:11:14 +010030struct poll_table_struct;
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static ssize_t default_read_file(struct file *file, char __user *buf,
33 size_t count, loff_t *ppos)
34{
35 return 0;
36}
37
38static ssize_t default_write_file(struct file *file, const char __user *buf,
39 size_t count, loff_t *ppos)
40{
41 return count;
42}
43
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010044const struct file_operations debugfs_noop_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .read = default_read_file,
46 .write = default_write_file,
Stephen Boyd234e3402012-04-05 14:25:11 -070047 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020048 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010051/**
52 * debugfs_use_file_start - mark the beginning of file data access
53 * @dentry: the dentry object whose data is being accessed.
54 * @srcu_idx: a pointer to some memory to store a SRCU index in.
55 *
56 * Up to a matching call to debugfs_use_file_finish(), any
57 * successive call into the file removing functions debugfs_remove()
58 * and debugfs_remove_recursive() will block. Since associated private
59 * file data may only get freed after a successful return of any of
60 * the removal functions, you may safely access it after a successful
61 * call to debugfs_use_file_start() without worrying about
62 * lifetime issues.
63 *
64 * If -%EIO is returned, the file has already been removed and thus,
65 * it is not safe to access any of its data. If, on the other hand,
66 * it is allowed to access the file data, zero is returned.
67 *
68 * Regardless of the return code, any call to
69 * debugfs_use_file_start() must be followed by a matching call
70 * to debugfs_use_file_finish().
71 */
Nicolai Stange49d200d2016-03-22 14:11:14 +010072int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010073 __acquires(&debugfs_srcu)
74{
75 *srcu_idx = srcu_read_lock(&debugfs_srcu);
76 barrier();
77 if (d_unlinked(dentry))
78 return -EIO;
79 return 0;
80}
Nicolai Stange49d200d2016-03-22 14:11:14 +010081EXPORT_SYMBOL_GPL(debugfs_use_file_start);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010082
83/**
84 * debugfs_use_file_finish - mark the end of file data access
85 * @srcu_idx: the SRCU index "created" by a former call to
86 * debugfs_use_file_start().
87 *
88 * Allow any ongoing concurrent call into debugfs_remove() or
89 * debugfs_remove_recursive() blocked by a former call to
90 * debugfs_use_file_start() to proceed and return to its caller.
91 */
Nicolai Stange49d200d2016-03-22 14:11:14 +010092void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu)
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010093{
94 srcu_read_unlock(&debugfs_srcu, srcu_idx);
95}
Nicolai Stange49d200d2016-03-22 14:11:14 +010096EXPORT_SYMBOL_GPL(debugfs_use_file_finish);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010097
98#define F_DENTRY(filp) ((filp)->f_path.dentry)
99
100#define REAL_FOPS_DEREF(dentry) \
101 ((const struct file_operations *)(dentry)->d_fsdata)
102
103static int open_proxy_open(struct inode *inode, struct file *filp)
104{
105 const struct dentry *dentry = F_DENTRY(filp);
106 const struct file_operations *real_fops = NULL;
107 int srcu_idx, r;
108
109 r = debugfs_use_file_start(dentry, &srcu_idx);
110 if (r) {
111 r = -ENOENT;
112 goto out;
113 }
114
115 real_fops = REAL_FOPS_DEREF(dentry);
116 real_fops = fops_get(real_fops);
117 if (!real_fops) {
118 /* Huh? Module did not clean up after itself at exit? */
119 WARN(1, "debugfs file owner did not clean up at exit: %pd",
120 dentry);
121 r = -ENXIO;
122 goto out;
123 }
124 replace_fops(filp, real_fops);
125
126 if (real_fops->open)
127 r = real_fops->open(inode, filp);
128
129out:
130 fops_put(real_fops);
131 debugfs_use_file_finish(srcu_idx);
132 return r;
133}
134
135const struct file_operations debugfs_open_proxy_file_operations = {
136 .open = open_proxy_open,
137};
138
Nicolai Stange49d200d2016-03-22 14:11:14 +0100139#define PROTO(args...) args
140#define ARGS(args...) args
141
142#define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
143static ret_type full_proxy_ ## name(proto) \
144{ \
145 const struct dentry *dentry = F_DENTRY(filp); \
146 const struct file_operations *real_fops = \
147 REAL_FOPS_DEREF(dentry); \
148 int srcu_idx; \
149 ret_type r; \
150 \
151 r = debugfs_use_file_start(dentry, &srcu_idx); \
152 if (likely(!r)) \
153 r = real_fops->name(args); \
154 debugfs_use_file_finish(srcu_idx); \
155 return r; \
156}
157
158FULL_PROXY_FUNC(llseek, loff_t, filp,
159 PROTO(struct file *filp, loff_t offset, int whence),
160 ARGS(filp, offset, whence));
161
162FULL_PROXY_FUNC(read, ssize_t, filp,
163 PROTO(struct file *filp, char __user *buf, size_t size,
164 loff_t *ppos),
165 ARGS(filp, buf, size, ppos));
166
167FULL_PROXY_FUNC(write, ssize_t, filp,
168 PROTO(struct file *filp, const char __user *buf, size_t size,
169 loff_t *ppos),
170 ARGS(filp, buf, size, ppos));
171
172FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
173 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
174 ARGS(filp, cmd, arg));
175
176static unsigned int full_proxy_poll(struct file *filp,
177 struct poll_table_struct *wait)
178{
179 const struct dentry *dentry = F_DENTRY(filp);
180 const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry);
181 int srcu_idx;
182 unsigned int r = 0;
183
184 if (debugfs_use_file_start(dentry, &srcu_idx)) {
185 debugfs_use_file_finish(srcu_idx);
186 return POLLHUP;
187 }
188
189 r = real_fops->poll(filp, wait);
190 debugfs_use_file_finish(srcu_idx);
191 return r;
192}
193
194static int full_proxy_release(struct inode *inode, struct file *filp)
195{
196 const struct dentry *dentry = F_DENTRY(filp);
197 const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry);
198 const struct file_operations *proxy_fops = filp->f_op;
199 int r = 0;
200
201 /*
202 * We must not protect this against removal races here: the
203 * original releaser should be called unconditionally in order
204 * not to leak any resources. Releasers must not assume that
205 * ->i_private is still being meaningful here.
206 */
207 if (real_fops->release)
208 r = real_fops->release(inode, filp);
209
210 replace_fops(filp, d_inode(dentry)->i_fop);
211 kfree((void *)proxy_fops);
212 fops_put(real_fops);
213 return 0;
214}
215
216static void __full_proxy_fops_init(struct file_operations *proxy_fops,
217 const struct file_operations *real_fops)
218{
219 proxy_fops->release = full_proxy_release;
220 if (real_fops->llseek)
221 proxy_fops->llseek = full_proxy_llseek;
222 if (real_fops->read)
223 proxy_fops->read = full_proxy_read;
224 if (real_fops->write)
225 proxy_fops->write = full_proxy_write;
226 if (real_fops->poll)
227 proxy_fops->poll = full_proxy_poll;
228 if (real_fops->unlocked_ioctl)
229 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
230}
231
232static int full_proxy_open(struct inode *inode, struct file *filp)
233{
234 const struct dentry *dentry = F_DENTRY(filp);
235 const struct file_operations *real_fops = NULL;
236 struct file_operations *proxy_fops = NULL;
237 int srcu_idx, r;
238
239 r = debugfs_use_file_start(dentry, &srcu_idx);
240 if (r) {
241 r = -ENOENT;
242 goto out;
243 }
244
245 real_fops = REAL_FOPS_DEREF(dentry);
246 real_fops = fops_get(real_fops);
247 if (!real_fops) {
248 /* Huh? Module did not cleanup after itself at exit? */
249 WARN(1, "debugfs file owner did not clean up at exit: %pd",
250 dentry);
251 r = -ENXIO;
252 goto out;
253 }
254
255 proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
256 if (!proxy_fops) {
257 r = -ENOMEM;
258 goto free_proxy;
259 }
260 __full_proxy_fops_init(proxy_fops, real_fops);
261 replace_fops(filp, proxy_fops);
262
263 if (real_fops->open) {
264 r = real_fops->open(inode, filp);
265
266 if (filp->f_op != proxy_fops) {
267 /* No protection against file removal anymore. */
268 WARN(1, "debugfs file owner replaced proxy fops: %pd",
269 dentry);
270 goto free_proxy;
271 }
272 }
273
274 goto out;
275free_proxy:
276 kfree(proxy_fops);
277 fops_put(real_fops);
278out:
279 debugfs_use_file_finish(srcu_idx);
280 return r;
281}
282
283const struct file_operations debugfs_full_proxy_file_operations = {
284 .open = full_proxy_open,
285};
286
Stephen Boydb97f6792015-10-12 18:09:09 -0700287static struct dentry *debugfs_create_mode(const char *name, umode_t mode,
288 struct dentry *parent, void *value,
289 const struct file_operations *fops,
290 const struct file_operations *fops_ro,
291 const struct file_operations *fops_wo)
292{
293 /* if there are no write bits set, make read only */
294 if (!(mode & S_IWUGO))
295 return debugfs_create_file(name, mode, parent, value, fops_ro);
296 /* if there are no read bits set, make write only */
297 if (!(mode & S_IRUGO))
298 return debugfs_create_file(name, mode, parent, value, fops_wo);
299
300 return debugfs_create_file(name, mode, parent, value, fops);
301}
302
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800303static int debugfs_u8_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200304{
305 *(u8 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800306 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200307}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800308static int debugfs_u8_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200309{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800310 *val = *(u8 *)data;
311 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200312}
313DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400314DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
315DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700318 * 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 -0700319 * @name: a pointer to a string containing the name of the file to create.
320 * @mode: the permission that the file should have
321 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700322 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * file will be created in the root of the debugfs filesystem.
324 * @value: a pointer to the variable that the file should read to and write
325 * from.
326 *
327 * This function creates a file in debugfs with the given name that
328 * contains the value of the variable @value. If the @mode variable is so
329 * set, it can be read from, and written to.
330 *
331 * This function will return a pointer to a dentry if it succeeds. This
332 * pointer must be passed to the debugfs_remove() function when the file is
333 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700334 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700336 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700338 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * code.
340 */
Al Virof4ae40a62011-07-24 04:33:43 -0400341struct dentry *debugfs_create_u8(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct dentry *parent, u8 *value)
343{
Stephen Boydb97f6792015-10-12 18:09:09 -0700344 return debugfs_create_mode(name, mode, parent, value, &fops_u8,
345 &fops_u8_ro, &fops_u8_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347EXPORT_SYMBOL_GPL(debugfs_create_u8);
348
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800349static int debugfs_u16_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200350{
351 *(u16 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800352 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200353}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800354static int debugfs_u16_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200355{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800356 *val = *(u16 *)data;
357 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200358}
359DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400360DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
361DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700364 * 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 -0700365 * @name: a pointer to a string containing the name of the file to create.
366 * @mode: the permission that the file should have
367 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700368 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * file will be created in the root of the debugfs filesystem.
370 * @value: a pointer to the variable that the file should read to and write
371 * from.
372 *
373 * This function creates a file in debugfs with the given name that
374 * contains the value of the variable @value. If the @mode variable is so
375 * set, it can be read from, and written to.
376 *
377 * This function will return a pointer to a dentry if it succeeds. This
378 * pointer must be passed to the debugfs_remove() function when the file is
379 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700380 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700382 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700384 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * code.
386 */
Al Virof4ae40a62011-07-24 04:33:43 -0400387struct dentry *debugfs_create_u16(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct dentry *parent, u16 *value)
389{
Stephen Boydb97f6792015-10-12 18:09:09 -0700390 return debugfs_create_mode(name, mode, parent, value, &fops_u16,
391 &fops_u16_ro, &fops_u16_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393EXPORT_SYMBOL_GPL(debugfs_create_u16);
394
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800395static int debugfs_u32_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200396{
397 *(u32 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800398 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200399}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800400static int debugfs_u32_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200401{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800402 *val = *(u32 *)data;
403 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200404}
405DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400406DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
407DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700410 * 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 -0700411 * @name: a pointer to a string containing the name of the file to create.
412 * @mode: the permission that the file should have
413 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700414 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * file will be created in the root of the debugfs filesystem.
416 * @value: a pointer to the variable that the file should read to and write
417 * from.
418 *
419 * This function creates a file in debugfs with the given name that
420 * contains the value of the variable @value. If the @mode variable is so
421 * set, it can be read from, and written to.
422 *
423 * This function will return a pointer to a dentry if it succeeds. This
424 * pointer must be passed to the debugfs_remove() function when the file is
425 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700426 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700428 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700430 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * code.
432 */
Al Virof4ae40a62011-07-24 04:33:43 -0400433struct dentry *debugfs_create_u32(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct dentry *parent, u32 *value)
435{
Stephen Boydb97f6792015-10-12 18:09:09 -0700436 return debugfs_create_mode(name, mode, parent, value, &fops_u32,
437 &fops_u32_ro, &fops_u32_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439EXPORT_SYMBOL_GPL(debugfs_create_u32);
440
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800441static int debugfs_u64_set(void *data, u64 val)
Michael Ellerman84478912007-04-17 15:59:36 +1000442{
443 *(u64 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800444 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000445}
446
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800447static int debugfs_u64_get(void *data, u64 *val)
Michael Ellerman84478912007-04-17 15:59:36 +1000448{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800449 *val = *(u64 *)data;
450 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000451}
452DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400453DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
454DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
Michael Ellerman84478912007-04-17 15:59:36 +1000455
456/**
457 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
458 * @name: a pointer to a string containing the name of the file to create.
459 * @mode: the permission that the file should have
460 * @parent: a pointer to the parent dentry for this file. This should be a
461 * directory dentry if set. If this parameter is %NULL, then the
462 * file will be created in the root of the debugfs filesystem.
463 * @value: a pointer to the variable that the file should read to and write
464 * from.
465 *
466 * This function creates a file in debugfs with the given name that
467 * contains the value of the variable @value. If the @mode variable is so
468 * set, it can be read from, and written to.
469 *
470 * This function will return a pointer to a dentry if it succeeds. This
471 * pointer must be passed to the debugfs_remove() function when the file is
472 * to be removed (no automatic cleanup happens if your module is unloaded,
473 * you are responsible here.) If an error occurs, %NULL will be returned.
474 *
475 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
476 * returned. It is not wise to check for this value, but rather, check for
477 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
478 * code.
479 */
Al Virof4ae40a62011-07-24 04:33:43 -0400480struct dentry *debugfs_create_u64(const char *name, umode_t mode,
Michael Ellerman84478912007-04-17 15:59:36 +1000481 struct dentry *parent, u64 *value)
482{
Stephen Boydb97f6792015-10-12 18:09:09 -0700483 return debugfs_create_mode(name, mode, parent, value, &fops_u64,
484 &fops_u64_ro, &fops_u64_wo);
Michael Ellerman84478912007-04-17 15:59:36 +1000485}
486EXPORT_SYMBOL_GPL(debugfs_create_u64);
487
Viresh Kumarc23fe832015-10-18 22:43:19 +0530488static int debugfs_ulong_set(void *data, u64 val)
489{
490 *(unsigned long *)data = val;
491 return 0;
492}
493
494static int debugfs_ulong_get(void *data, u64 *val)
495{
496 *val = *(unsigned long *)data;
497 return 0;
498}
499DEFINE_SIMPLE_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, "%llu\n");
500DEFINE_SIMPLE_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
501DEFINE_SIMPLE_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
502
503/**
504 * debugfs_create_ulong - create a debugfs file that is used to read and write
505 * an unsigned long value.
506 * @name: a pointer to a string containing the name of the file to create.
507 * @mode: the permission that the file should have
508 * @parent: a pointer to the parent dentry for this file. This should be a
509 * directory dentry if set. If this parameter is %NULL, then the
510 * file will be created in the root of the debugfs filesystem.
511 * @value: a pointer to the variable that the file should read to and write
512 * from.
513 *
514 * This function creates a file in debugfs with the given name that
515 * contains the value of the variable @value. If the @mode variable is so
516 * set, it can be read from, and written to.
517 *
518 * This function will return a pointer to a dentry if it succeeds. This
519 * pointer must be passed to the debugfs_remove() function when the file is
520 * to be removed (no automatic cleanup happens if your module is unloaded,
521 * you are responsible here.) If an error occurs, %NULL will be returned.
522 *
523 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
524 * returned. It is not wise to check for this value, but rather, check for
525 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
526 * code.
527 */
528struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
529 struct dentry *parent, unsigned long *value)
530{
531 return debugfs_create_mode(name, mode, parent, value, &fops_ulong,
532 &fops_ulong_ro, &fops_ulong_wo);
533}
534EXPORT_SYMBOL_GPL(debugfs_create_ulong);
535
Robin Getz2ebefc52007-08-02 18:23:50 -0400536DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400537DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
538DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400539
540DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400541DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
542DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400543
544DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400545DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
546DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400547
Huang Ying15b0bea2010-05-18 14:35:23 +0800548DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
Stephen Boyd82b7d4f2015-10-12 18:09:10 -0700549DEFINE_SIMPLE_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
550DEFINE_SIMPLE_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
Huang Ying15b0bea2010-05-18 14:35:23 +0800551
Randy Dunlape6716b82007-10-15 17:30:19 -0700552/*
Huang Ying15b0bea2010-05-18 14:35:23 +0800553 * 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 -0700554 *
555 * These functions are exactly the same as the above functions (but use a hex
556 * output for the decimal challenged). For details look at the above unsigned
557 * decimal functions.
558 */
559
Robin Getz2ebefc52007-08-02 18:23:50 -0400560/**
561 * 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 -0700562 * @name: a pointer to a string containing the name of the file to create.
563 * @mode: the permission that the file should have
564 * @parent: a pointer to the parent dentry for this file. This should be a
565 * directory dentry if set. If this parameter is %NULL, then the
566 * file will be created in the root of the debugfs filesystem.
567 * @value: a pointer to the variable that the file should read to and write
568 * from.
Robin Getz2ebefc52007-08-02 18:23:50 -0400569 */
Al Virof4ae40a62011-07-24 04:33:43 -0400570struct dentry *debugfs_create_x8(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400571 struct dentry *parent, u8 *value)
572{
Stephen Boydb97f6792015-10-12 18:09:09 -0700573 return debugfs_create_mode(name, mode, parent, value, &fops_x8,
574 &fops_x8_ro, &fops_x8_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400575}
576EXPORT_SYMBOL_GPL(debugfs_create_x8);
577
Randy Dunlape6716b82007-10-15 17:30:19 -0700578/**
579 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
580 * @name: a pointer to a string containing the name of the file to create.
581 * @mode: the permission that the file should have
582 * @parent: a pointer to the parent dentry for this file. This should be a
583 * directory dentry if set. If this parameter is %NULL, then the
584 * file will be created in the root of the debugfs filesystem.
585 * @value: a pointer to the variable that the file should read to and write
586 * from.
587 */
Al Virof4ae40a62011-07-24 04:33:43 -0400588struct dentry *debugfs_create_x16(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400589 struct dentry *parent, u16 *value)
590{
Stephen Boydb97f6792015-10-12 18:09:09 -0700591 return debugfs_create_mode(name, mode, parent, value, &fops_x16,
592 &fops_x16_ro, &fops_x16_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400593}
594EXPORT_SYMBOL_GPL(debugfs_create_x16);
595
Randy Dunlape6716b82007-10-15 17:30:19 -0700596/**
597 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
598 * @name: a pointer to a string containing the name of the file to create.
599 * @mode: the permission that the file should have
600 * @parent: a pointer to the parent dentry for this file. This should be a
601 * directory dentry if set. If this parameter is %NULL, then the
602 * file will be created in the root of the debugfs filesystem.
603 * @value: a pointer to the variable that the file should read to and write
604 * from.
605 */
Al Virof4ae40a62011-07-24 04:33:43 -0400606struct dentry *debugfs_create_x32(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400607 struct dentry *parent, u32 *value)
608{
Stephen Boydb97f6792015-10-12 18:09:09 -0700609 return debugfs_create_mode(name, mode, parent, value, &fops_x32,
610 &fops_x32_ro, &fops_x32_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400611}
612EXPORT_SYMBOL_GPL(debugfs_create_x32);
613
Huang Ying15b0bea2010-05-18 14:35:23 +0800614/**
615 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
616 * @name: a pointer to a string containing the name of the file to create.
617 * @mode: the permission that the file should have
618 * @parent: a pointer to the parent dentry for this file. This should be a
619 * directory dentry if set. If this parameter is %NULL, then the
620 * file will be created in the root of the debugfs filesystem.
621 * @value: a pointer to the variable that the file should read to and write
622 * from.
623 */
Al Virof4ae40a62011-07-24 04:33:43 -0400624struct dentry *debugfs_create_x64(const char *name, umode_t mode,
Huang Ying15b0bea2010-05-18 14:35:23 +0800625 struct dentry *parent, u64 *value)
626{
Stephen Boyd82b7d4f2015-10-12 18:09:10 -0700627 return debugfs_create_mode(name, mode, parent, value, &fops_x64,
628 &fops_x64_ro, &fops_x64_wo);
Huang Ying15b0bea2010-05-18 14:35:23 +0800629}
630EXPORT_SYMBOL_GPL(debugfs_create_x64);
631
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800632
633static int debugfs_size_t_set(void *data, u64 val)
634{
635 *(size_t *)data = val;
636 return 0;
637}
638static int debugfs_size_t_get(void *data, u64 *val)
639{
640 *val = *(size_t *)data;
641 return 0;
642}
643DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
644 "%llu\n"); /* %llu and %zu are more or less the same */
Stephen Boyd6db66522015-10-12 18:09:11 -0700645DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
646DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800647
648/**
649 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
650 * @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
653 * directory dentry if set. If this parameter is %NULL, then the
654 * file will be created in the root of the debugfs filesystem.
655 * @value: a pointer to the variable that the file should read to and write
656 * from.
657 */
Al Virof4ae40a62011-07-24 04:33:43 -0400658struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800659 struct dentry *parent, size_t *value)
660{
Stephen Boyd6db66522015-10-12 18:09:11 -0700661 return debugfs_create_mode(name, mode, parent, value, &fops_size_t,
662 &fops_size_t_ro, &fops_size_t_wo);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800663}
664EXPORT_SYMBOL_GPL(debugfs_create_size_t);
665
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500666static int debugfs_atomic_t_set(void *data, u64 val)
667{
668 atomic_set((atomic_t *)data, val);
669 return 0;
670}
671static int debugfs_atomic_t_get(void *data, u64 *val)
672{
673 *val = atomic_read((atomic_t *)data);
674 return 0;
675}
676DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
677 debugfs_atomic_t_set, "%lld\n");
678DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
679DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
680
681/**
682 * debugfs_create_atomic_t - create a debugfs file that is used to read and
683 * write an atomic_t value
684 * @name: a pointer to a string containing the name of the file to create.
685 * @mode: the permission that the file should have
686 * @parent: a pointer to the parent dentry for this file. This should be a
687 * directory dentry if set. If this parameter is %NULL, then the
688 * file will be created in the root of the debugfs filesystem.
689 * @value: a pointer to the variable that the file should read to and write
690 * from.
691 */
692struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
693 struct dentry *parent, atomic_t *value)
694{
Stephen Boydb97f6792015-10-12 18:09:09 -0700695 return debugfs_create_mode(name, mode, parent, value, &fops_atomic_t,
696 &fops_atomic_t_ro, &fops_atomic_t_wo);
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500697}
698EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800699
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100700ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
701 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 char buf[3];
Viresh Kumar621a5f72015-09-26 15:04:07 -0700704 bool *val = file->private_data;
Rahul Bedarkar88e412e2014-06-06 23:12:04 +0530705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (*val)
707 buf[0] = 'Y';
708 else
709 buf[0] = 'N';
710 buf[1] = '\n';
711 buf[2] = 0x00;
712 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
713}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100714EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100716ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
717 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 char buf[32];
Stephen Boydc42d2232011-05-12 16:50:07 -0700720 size_t buf_size;
Jonathan Cameron8705b482011-04-19 12:43:46 +0100721 bool bv;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700722 bool *val = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 buf_size = min(count, (sizeof(buf)-1));
725 if (copy_from_user(buf, user_buf, buf_size))
726 return -EFAULT;
727
Mathias Krausea3b2c8c72013-05-31 23:24:29 +0200728 buf[buf_size] = '\0';
Jonathan Cameron8705b482011-04-19 12:43:46 +0100729 if (strtobool(buf, &bv) == 0)
730 *val = bv;
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return count;
733}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100734EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800736static const struct file_operations fops_bool = {
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100737 .read = debugfs_read_file_bool,
738 .write = debugfs_write_file_bool,
Stephen Boyd234e3402012-04-05 14:25:11 -0700739 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200740 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741};
742
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700743static const struct file_operations fops_bool_ro = {
744 .read = debugfs_read_file_bool,
745 .open = simple_open,
746 .llseek = default_llseek,
747};
748
749static const struct file_operations fops_bool_wo = {
750 .write = debugfs_write_file_bool,
751 .open = simple_open,
752 .llseek = default_llseek,
753};
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700756 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * @name: a pointer to a string containing the name of the file to create.
758 * @mode: the permission that the file should have
759 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700760 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * file will be created in the root of the debugfs filesystem.
762 * @value: a pointer to the variable that the file should read to and write
763 * from.
764 *
765 * This function creates a file in debugfs with the given name that
766 * contains the value of the variable @value. If the @mode variable is so
767 * set, it can be read from, and written to.
768 *
769 * This function will return a pointer to a dentry if it succeeds. This
770 * pointer must be passed to the debugfs_remove() function when the file is
771 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700772 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700774 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700776 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 * code.
778 */
Al Virof4ae40a62011-07-24 04:33:43 -0400779struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700780 struct dentry *parent, bool *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700782 return debugfs_create_mode(name, mode, parent, value, &fops_bool,
783 &fops_bool_ro, &fops_bool_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785EXPORT_SYMBOL_GPL(debugfs_create_bool);
786
Michael Ellermandd308bc2006-03-07 21:41:59 +1100787static ssize_t read_file_blob(struct file *file, char __user *user_buf,
788 size_t count, loff_t *ppos)
789{
790 struct debugfs_blob_wrapper *blob = file->private_data;
791 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
792 blob->size);
793}
794
Arjan van de Ven00977a52007-02-12 00:55:34 -0800795static const struct file_operations fops_blob = {
Michael Ellermandd308bc2006-03-07 21:41:59 +1100796 .read = read_file_blob,
Stephen Boyd234e3402012-04-05 14:25:11 -0700797 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200798 .llseek = default_llseek,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100799};
800
801/**
Jonathan Corbet400ced62009-05-25 10:15:27 -0600802 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
Michael Ellermandd308bc2006-03-07 21:41:59 +1100803 * @name: a pointer to a string containing the name of the file to create.
804 * @mode: the permission that the file should have
805 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700806 * directory dentry if set. If this parameter is %NULL, then the
Michael Ellermandd308bc2006-03-07 21:41:59 +1100807 * file will be created in the root of the debugfs filesystem.
808 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
809 * to the blob data and the size of the data.
810 *
811 * This function creates a file in debugfs with the given name that exports
812 * @blob->data as a binary blob. If the @mode variable is so set it can be
813 * read from. Writing is not supported.
814 *
815 * This function will return a pointer to a dentry if it succeeds. This
816 * pointer must be passed to the debugfs_remove() function when the file is
817 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700818 * you are responsible here.) If an error occurs, %NULL will be returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +1100819 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700820 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Michael Ellermandd308bc2006-03-07 21:41:59 +1100821 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700822 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Michael Ellermandd308bc2006-03-07 21:41:59 +1100823 * code.
824 */
Al Virof4ae40a62011-07-24 04:33:43 -0400825struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100826 struct dentry *parent,
827 struct debugfs_blob_wrapper *blob)
828{
829 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
830}
831EXPORT_SYMBOL_GPL(debugfs_create_blob);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100832
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530833struct array_data {
834 void *array;
835 u32 elements;
836};
837
Linus Torvaldse05e2792012-09-21 11:48:05 -0700838static size_t u32_format_array(char *buf, size_t bufsize,
839 u32 *array, int array_size)
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530840{
841 size_t ret = 0;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530842
Linus Torvaldse05e2792012-09-21 11:48:05 -0700843 while (--array_size >= 0) {
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530844 size_t len;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700845 char term = array_size ? ' ' : '\n';
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530846
Linus Torvaldse05e2792012-09-21 11:48:05 -0700847 len = snprintf(buf, bufsize, "%u%c", *array++, term);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530848 ret += len;
849
Linus Torvaldse05e2792012-09-21 11:48:05 -0700850 buf += len;
851 bufsize -= len;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530852 }
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530853 return ret;
854}
855
David Rientjes36048852012-09-21 02:16:29 -0700856static int u32_array_open(struct inode *inode, struct file *file)
857{
858 struct array_data *data = inode->i_private;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700859 int size, elements = data->elements;
860 char *buf;
David Rientjes36048852012-09-21 02:16:29 -0700861
Linus Torvaldse05e2792012-09-21 11:48:05 -0700862 /*
863 * Max size:
864 * - 10 digits + ' '/'\n' = 11 bytes per number
865 * - terminating NUL character
866 */
867 size = elements*11;
868 buf = kmalloc(size+1, GFP_KERNEL);
869 if (!buf)
David Rientjes36048852012-09-21 02:16:29 -0700870 return -ENOMEM;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700871 buf[size] = 0;
872
873 file->private_data = buf;
874 u32_format_array(buf, size, data->array, data->elements);
875
David Rientjes36048852012-09-21 02:16:29 -0700876 return nonseekable_open(inode, file);
877}
878
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530879static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
880 loff_t *ppos)
881{
David Rientjes36048852012-09-21 02:16:29 -0700882 size_t size = strlen(file->private_data);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530883
884 return simple_read_from_buffer(buf, len, ppos,
885 file->private_data, size);
886}
887
888static int u32_array_release(struct inode *inode, struct file *file)
889{
890 kfree(file->private_data);
891
892 return 0;
893}
894
895static const struct file_operations u32_array_fops = {
896 .owner = THIS_MODULE,
897 .open = u32_array_open,
898 .release = u32_array_release,
899 .read = u32_array_read,
900 .llseek = no_llseek,
901};
902
903/**
904 * debugfs_create_u32_array - create a debugfs file that is used to read u32
905 * array.
906 * @name: a pointer to a string containing the name of the file to create.
907 * @mode: the permission that the file should have.
908 * @parent: a pointer to the parent dentry for this file. This should be a
909 * directory dentry if set. If this parameter is %NULL, then the
910 * file will be created in the root of the debugfs filesystem.
911 * @array: u32 array that provides data.
912 * @elements: total number of elements in the array.
913 *
914 * This function creates a file in debugfs with the given name that exports
915 * @array as data. If the @mode variable is so set it can be read from.
916 * Writing is not supported. Seek within the file is also not supported.
917 * Once array is created its size can not be changed.
918 *
919 * The function returns a pointer to dentry on success. If debugfs is not
920 * enabled in the kernel, the value -%ENODEV will be returned.
921 */
922struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
923 struct dentry *parent,
924 u32 *array, u32 elements)
925{
926 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
927
928 if (data == NULL)
929 return NULL;
930
931 data->array = array;
932 data->elements = elements;
933
934 return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
935}
936EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
937
Heiko Carstens3b85e4a2011-12-27 15:08:28 +0100938#ifdef CONFIG_HAS_IOMEM
939
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100940/*
941 * The regset32 stuff is used to print 32-bit registers using the
942 * seq_file utilities. We offer printing a register set in an already-opened
943 * sequential file or create a debugfs file that only prints a regset32.
944 */
945
946/**
947 * debugfs_print_regs32 - use seq_print to describe a set of registers
948 * @s: the seq_file structure being used to generate output
949 * @regs: an array if struct debugfs_reg32 structures
Randy Dunlapb5763ac2012-01-21 11:02:42 -0800950 * @nregs: the length of the above array
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100951 * @base: the base address to be used in reading the registers
952 * @prefix: a string to be prefixed to every output line
953 *
954 * This function outputs a text block describing the current values of
955 * some 32-bit hardware registers. It is meant to be used within debugfs
956 * files based on seq_file that need to show registers, intermixed with other
957 * information. The prefix argument may be used to specify a leading string,
958 * because some peripherals have several blocks of identical registers,
959 * for example configuration of dma channels
960 */
Joe Perches97615362014-09-29 16:08:26 -0700961void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
962 int nregs, void __iomem *base, char *prefix)
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100963{
Joe Perches97615362014-09-29 16:08:26 -0700964 int i;
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100965
966 for (i = 0; i < nregs; i++, regs++) {
967 if (prefix)
Joe Perches97615362014-09-29 16:08:26 -0700968 seq_printf(s, "%s", prefix);
969 seq_printf(s, "%s = 0x%08x\n", regs->name,
970 readl(base + regs->offset));
971 if (seq_has_overflowed(s))
972 break;
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100973 }
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100974}
975EXPORT_SYMBOL_GPL(debugfs_print_regs32);
976
977static int debugfs_show_regset32(struct seq_file *s, void *data)
978{
979 struct debugfs_regset32 *regset = s->private;
980
981 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
982 return 0;
983}
984
985static int debugfs_open_regset32(struct inode *inode, struct file *file)
986{
987 return single_open(file, debugfs_show_regset32, inode->i_private);
988}
989
990static const struct file_operations fops_regset32 = {
991 .open = debugfs_open_regset32,
992 .read = seq_read,
993 .llseek = seq_lseek,
994 .release = single_release,
995};
996
997/**
998 * debugfs_create_regset32 - create a debugfs file that returns register values
999 * @name: a pointer to a string containing the name of the file to create.
1000 * @mode: the permission that the file should have
1001 * @parent: a pointer to the parent dentry for this file. This should be a
1002 * directory dentry if set. If this parameter is %NULL, then the
1003 * file will be created in the root of the debugfs filesystem.
1004 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1005 * to an array of register definitions, the array size and the base
1006 * address where the register bank is to be found.
1007 *
1008 * This function creates a file in debugfs with the given name that reports
1009 * the names and values of a set of 32-bit registers. If the @mode variable
1010 * is so set it can be read from. Writing is not supported.
1011 *
1012 * This function will return a pointer to a dentry if it succeeds. This
1013 * pointer must be passed to the debugfs_remove() function when the file is
1014 * to be removed (no automatic cleanup happens if your module is unloaded,
1015 * you are responsible here.) If an error occurs, %NULL will be returned.
1016 *
1017 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1018 * returned. It is not wise to check for this value, but rather, check for
1019 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1020 * code.
1021 */
Al Viro88187392012-03-20 06:00:24 -04001022struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001023 struct dentry *parent,
1024 struct debugfs_regset32 *regset)
1025{
1026 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
1027}
1028EXPORT_SYMBOL_GPL(debugfs_create_regset32);
Heiko Carstens3b85e4a2011-12-27 15:08:28 +01001029
1030#endif /* CONFIG_HAS_IOMEM */
Arend van Spriel98210b72014-11-09 11:31:58 +01001031
1032struct debugfs_devm_entry {
1033 int (*read)(struct seq_file *seq, void *data);
1034 struct device *dev;
1035};
1036
1037static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1038{
1039 struct debugfs_devm_entry *entry = inode->i_private;
1040
1041 return single_open(f, entry->read, entry->dev);
1042}
1043
1044static const struct file_operations debugfs_devm_entry_ops = {
1045 .owner = THIS_MODULE,
1046 .open = debugfs_devm_entry_open,
1047 .release = single_release,
1048 .read = seq_read,
1049 .llseek = seq_lseek
1050};
1051
1052/**
1053 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1054 *
1055 * @dev: device related to this debugfs file.
1056 * @name: name of the debugfs file.
1057 * @parent: a pointer to the parent dentry for this file. This should be a
1058 * directory dentry if set. If this parameter is %NULL, then the
1059 * file will be created in the root of the debugfs filesystem.
1060 * @read_fn: function pointer called to print the seq_file content.
1061 */
1062struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
1063 struct dentry *parent,
1064 int (*read_fn)(struct seq_file *s,
1065 void *data))
1066{
1067 struct debugfs_devm_entry *entry;
1068
1069 if (IS_ERR(parent))
1070 return ERR_PTR(-ENOENT);
1071
1072 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1073 if (!entry)
1074 return ERR_PTR(-ENOMEM);
1075
1076 entry->read = read_fn;
1077 entry->dev = dev;
1078
1079 return debugfs_create_file(name, S_IRUGO, parent, entry,
1080 &debugfs_devm_entry_ops);
1081}
1082EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1083