blob: bc3549c95574ad9d390879cb788b0dc5eb90ea9c [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.
Mauro Carvalho Chehabe1b4fc72017-05-14 12:04:55 -030012 * See Documentation/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 Stange49d200d2016-03-22 14:11:14 +010025#include <asm/poll.h>
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010026
27#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Nicolai Stange49d200d2016-03-22 14:11:14 +010029struct poll_table_struct;
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static ssize_t default_read_file(struct file *file, char __user *buf,
32 size_t count, loff_t *ppos)
33{
34 return 0;
35}
36
37static ssize_t default_write_file(struct file *file, const char __user *buf,
38 size_t count, loff_t *ppos)
39{
40 return count;
41}
42
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010043const struct file_operations debugfs_noop_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .read = default_read_file,
45 .write = default_write_file,
Stephen Boyd234e3402012-04-05 14:25:11 -070046 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020047 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010050#define F_DENTRY(filp) ((filp)->f_path.dentry)
51
Nicolai Stange7c8d4692017-10-31 00:15:47 +010052const struct file_operations *debugfs_real_fops(const struct file *filp)
Nicolai Stange7c8d4692017-10-31 00:15:47 +010053{
54 struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
Nicolai Stange055ab8e2017-10-31 00:15:49 +010055
Nicolai Stange7c8d4692017-10-31 00:15:47 +010056 return fsd->real_fops;
57}
58EXPORT_SYMBOL_GPL(debugfs_real_fops);
59
Nicolai Stangee9117a52017-10-31 00:15:48 +010060/**
61 * debugfs_file_get - mark the beginning of file data access
62 * @dentry: the dentry object whose data is being accessed.
63 *
64 * Up to a matching call to debugfs_file_put(), any successive call
65 * into the file removing functions debugfs_remove() and
66 * debugfs_remove_recursive() will block. Since associated private
67 * file data may only get freed after a successful return of any of
68 * the removal functions, you may safely access it after a successful
69 * call to debugfs_file_get() without worrying about lifetime issues.
70 *
71 * If -%EIO is returned, the file has already been removed and thus,
72 * it is not safe to access any of its data. If, on the other hand,
73 * it is allowed to access the file data, zero is returned.
74 */
75int debugfs_file_get(struct dentry *dentry)
76{
77 struct debugfs_fsdata *fsd = dentry->d_fsdata;
78
79 /* Avoid starvation of removers. */
80 if (d_unlinked(dentry))
81 return -EIO;
82
83 if (!refcount_inc_not_zero(&fsd->active_users))
84 return -EIO;
85
86 return 0;
87}
88EXPORT_SYMBOL_GPL(debugfs_file_get);
89
90/**
91 * debugfs_file_put - mark the end of file data access
92 * @dentry: the dentry object formerly passed to
93 * debugfs_file_get().
94 *
95 * Allow any ongoing concurrent call into debugfs_remove() or
96 * debugfs_remove_recursive() blocked by a former call to
97 * debugfs_file_get() to proceed and return to its caller.
98 */
99void debugfs_file_put(struct dentry *dentry)
100{
101 struct debugfs_fsdata *fsd = dentry->d_fsdata;
102
103 if (refcount_dec_and_test(&fsd->active_users))
104 complete(&fsd->active_users_drained);
105}
106EXPORT_SYMBOL_GPL(debugfs_file_put);
107
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100108static int open_proxy_open(struct inode *inode, struct file *filp)
109{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100110 struct dentry *dentry = F_DENTRY(filp);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100111 const struct file_operations *real_fops = NULL;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100112 int r = 0;
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100113
Nicolai Stange69d29f92017-10-31 00:15:50 +0100114 if (debugfs_file_get(dentry))
115 return -ENOENT;
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100116
Christian Lamparter86f0e062016-09-17 21:43:01 +0200117 real_fops = debugfs_real_fops(filp);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100118 real_fops = fops_get(real_fops);
119 if (!real_fops) {
120 /* Huh? Module did not clean up after itself at exit? */
121 WARN(1, "debugfs file owner did not clean up at exit: %pd",
122 dentry);
123 r = -ENXIO;
124 goto out;
125 }
126 replace_fops(filp, real_fops);
127
128 if (real_fops->open)
129 r = real_fops->open(inode, filp);
130
131out:
Nicolai Stange69d29f92017-10-31 00:15:50 +0100132 debugfs_file_put(dentry);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100133 return r;
134}
135
136const struct file_operations debugfs_open_proxy_file_operations = {
137 .open = open_proxy_open,
138};
139
Nicolai Stange49d200d2016-03-22 14:11:14 +0100140#define PROTO(args...) args
141#define ARGS(args...) args
142
143#define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
144static ret_type full_proxy_ ## name(proto) \
145{ \
Nicolai Stange69d29f92017-10-31 00:15:50 +0100146 struct dentry *dentry = F_DENTRY(filp); \
Nicolai Stange154b9d72017-10-31 00:15:53 +0100147 const struct file_operations *real_fops; \
Nicolai Stange49d200d2016-03-22 14:11:14 +0100148 ret_type r; \
149 \
Nicolai Stange69d29f92017-10-31 00:15:50 +0100150 r = debugfs_file_get(dentry); \
151 if (unlikely(r)) \
152 return r; \
Nicolai Stange154b9d72017-10-31 00:15:53 +0100153 real_fops = debugfs_real_fops(filp); \
Nicolai Stange69d29f92017-10-31 00:15:50 +0100154 r = real_fops->name(args); \
155 debugfs_file_put(dentry); \
Nicolai Stange49d200d2016-03-22 14:11:14 +0100156 return r; \
157}
158
159FULL_PROXY_FUNC(llseek, loff_t, filp,
160 PROTO(struct file *filp, loff_t offset, int whence),
161 ARGS(filp, offset, whence));
162
163FULL_PROXY_FUNC(read, ssize_t, filp,
164 PROTO(struct file *filp, char __user *buf, size_t size,
165 loff_t *ppos),
166 ARGS(filp, buf, size, ppos));
167
168FULL_PROXY_FUNC(write, ssize_t, filp,
169 PROTO(struct file *filp, const char __user *buf, size_t size,
170 loff_t *ppos),
171 ARGS(filp, buf, size, ppos));
172
173FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
174 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
175 ARGS(filp, cmd, arg));
176
177static unsigned int full_proxy_poll(struct file *filp,
178 struct poll_table_struct *wait)
179{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100180 struct dentry *dentry = F_DENTRY(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100181 unsigned int r = 0;
Nicolai Stange154b9d72017-10-31 00:15:53 +0100182 const struct file_operations *real_fops;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100183
Nicolai Stange69d29f92017-10-31 00:15:50 +0100184 if (debugfs_file_get(dentry))
Nicolai Stange49d200d2016-03-22 14:11:14 +0100185 return POLLHUP;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100186
Nicolai Stange154b9d72017-10-31 00:15:53 +0100187 real_fops = debugfs_real_fops(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100188 r = real_fops->poll(filp, wait);
Nicolai Stange69d29f92017-10-31 00:15:50 +0100189 debugfs_file_put(dentry);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100190 return r;
191}
192
193static int full_proxy_release(struct inode *inode, struct file *filp)
194{
195 const struct dentry *dentry = F_DENTRY(filp);
Christian Lamparter86f0e062016-09-17 21:43:01 +0200196 const struct file_operations *real_fops = debugfs_real_fops(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100197 const struct file_operations *proxy_fops = filp->f_op;
198 int r = 0;
199
200 /*
201 * We must not protect this against removal races here: the
202 * original releaser should be called unconditionally in order
203 * not to leak any resources. Releasers must not assume that
204 * ->i_private is still being meaningful here.
205 */
206 if (real_fops->release)
207 r = real_fops->release(inode, filp);
208
209 replace_fops(filp, d_inode(dentry)->i_fop);
210 kfree((void *)proxy_fops);
211 fops_put(real_fops);
Eric Engestroma1a9e5d2016-09-21 10:27:36 +0100212 return r;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100213}
214
215static void __full_proxy_fops_init(struct file_operations *proxy_fops,
216 const struct file_operations *real_fops)
217{
218 proxy_fops->release = full_proxy_release;
219 if (real_fops->llseek)
220 proxy_fops->llseek = full_proxy_llseek;
221 if (real_fops->read)
222 proxy_fops->read = full_proxy_read;
223 if (real_fops->write)
224 proxy_fops->write = full_proxy_write;
225 if (real_fops->poll)
226 proxy_fops->poll = full_proxy_poll;
227 if (real_fops->unlocked_ioctl)
228 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
229}
230
231static int full_proxy_open(struct inode *inode, struct file *filp)
232{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100233 struct dentry *dentry = F_DENTRY(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100234 const struct file_operations *real_fops = NULL;
235 struct file_operations *proxy_fops = NULL;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100236 int r = 0;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100237
Nicolai Stange69d29f92017-10-31 00:15:50 +0100238 if (debugfs_file_get(dentry))
239 return -ENOENT;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100240
Christian Lamparter86f0e062016-09-17 21:43:01 +0200241 real_fops = debugfs_real_fops(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100242 real_fops = fops_get(real_fops);
243 if (!real_fops) {
244 /* Huh? Module did not cleanup after itself at exit? */
245 WARN(1, "debugfs file owner did not clean up at exit: %pd",
246 dentry);
247 r = -ENXIO;
248 goto out;
249 }
250
251 proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
252 if (!proxy_fops) {
253 r = -ENOMEM;
254 goto free_proxy;
255 }
256 __full_proxy_fops_init(proxy_fops, real_fops);
257 replace_fops(filp, proxy_fops);
258
259 if (real_fops->open) {
260 r = real_fops->open(inode, filp);
Nicolai Stangeb10e3e92016-05-24 13:08:53 +0200261 if (r) {
262 replace_fops(filp, d_inode(dentry)->i_fop);
263 goto free_proxy;
264 } else if (filp->f_op != proxy_fops) {
Nicolai Stange49d200d2016-03-22 14:11:14 +0100265 /* No protection against file removal anymore. */
266 WARN(1, "debugfs file owner replaced proxy fops: %pd",
267 dentry);
268 goto free_proxy;
269 }
270 }
271
272 goto out;
273free_proxy:
274 kfree(proxy_fops);
275 fops_put(real_fops);
276out:
Nicolai Stange69d29f92017-10-31 00:15:50 +0100277 debugfs_file_put(dentry);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100278 return r;
279}
280
281const struct file_operations debugfs_full_proxy_file_operations = {
282 .open = full_proxy_open,
283};
284
Nicolai Stangec6468802016-03-22 14:11:15 +0100285ssize_t debugfs_attr_read(struct file *file, char __user *buf,
286 size_t len, loff_t *ppos)
287{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100288 struct dentry *dentry = F_DENTRY(file);
Nicolai Stangec6468802016-03-22 14:11:15 +0100289 ssize_t ret;
Nicolai Stangec6468802016-03-22 14:11:15 +0100290
Nicolai Stange69d29f92017-10-31 00:15:50 +0100291 ret = debugfs_file_get(dentry);
292 if (unlikely(ret))
293 return ret;
294 ret = simple_attr_read(file, buf, len, ppos);
295 debugfs_file_put(dentry);
Nicolai Stangec6468802016-03-22 14:11:15 +0100296 return ret;
297}
298EXPORT_SYMBOL_GPL(debugfs_attr_read);
299
300ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
301 size_t len, loff_t *ppos)
302{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100303 struct dentry *dentry = F_DENTRY(file);
Nicolai Stangec6468802016-03-22 14:11:15 +0100304 ssize_t ret;
Nicolai Stangec6468802016-03-22 14:11:15 +0100305
Nicolai Stange69d29f92017-10-31 00:15:50 +0100306 ret = debugfs_file_get(dentry);
307 if (unlikely(ret))
308 return ret;
309 ret = simple_attr_write(file, buf, len, ppos);
310 debugfs_file_put(dentry);
Nicolai Stangec6468802016-03-22 14:11:15 +0100311 return ret;
312}
313EXPORT_SYMBOL_GPL(debugfs_attr_write);
314
Nicolai Stange4909f162016-03-22 14:11:17 +0100315static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
316 struct dentry *parent, void *value,
317 const struct file_operations *fops,
318 const struct file_operations *fops_ro,
319 const struct file_operations *fops_wo)
320{
321 /* if there are no write bits set, make read only */
322 if (!(mode & S_IWUGO))
323 return debugfs_create_file_unsafe(name, mode, parent, value,
324 fops_ro);
325 /* if there are no read bits set, make write only */
326 if (!(mode & S_IRUGO))
327 return debugfs_create_file_unsafe(name, mode, parent, value,
328 fops_wo);
329
330 return debugfs_create_file_unsafe(name, mode, parent, value, fops);
331}
332
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800333static int debugfs_u8_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200334{
335 *(u8 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800336 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200337}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800338static int debugfs_u8_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200339{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800340 *val = *(u8 *)data;
341 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200342}
Nicolai Stange4909f162016-03-22 14:11:17 +0100343DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
344DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
345DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700348 * 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 -0700349 * @name: a pointer to a string containing the name of the file to create.
350 * @mode: the permission that the file should have
351 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700352 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * file will be created in the root of the debugfs filesystem.
354 * @value: a pointer to the variable that the file should read to and write
355 * from.
356 *
357 * This function creates a file in debugfs with the given name that
358 * contains the value of the variable @value. If the @mode variable is so
359 * set, it can be read from, and written to.
360 *
361 * This function will return a pointer to a dentry if it succeeds. This
362 * pointer must be passed to the debugfs_remove() function when the file is
363 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700364 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700366 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700368 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * code.
370 */
Al Virof4ae40a2011-07-24 04:33:43 -0400371struct dentry *debugfs_create_u8(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct dentry *parent, u8 *value)
373{
Nicolai Stange4909f162016-03-22 14:11:17 +0100374 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
Stephen Boydb97f6792015-10-12 18:09:09 -0700375 &fops_u8_ro, &fops_u8_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377EXPORT_SYMBOL_GPL(debugfs_create_u8);
378
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800379static int debugfs_u16_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200380{
381 *(u16 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800382 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200383}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800384static int debugfs_u16_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200385{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800386 *val = *(u16 *)data;
387 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200388}
Nicolai Stange4909f162016-03-22 14:11:17 +0100389DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
390DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
391DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700394 * 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 -0700395 * @name: a pointer to a string containing the name of the file to create.
396 * @mode: the permission that the file should have
397 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700398 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * file will be created in the root of the debugfs filesystem.
400 * @value: a pointer to the variable that the file should read to and write
401 * from.
402 *
403 * This function creates a file in debugfs with the given name that
404 * contains the value of the variable @value. If the @mode variable is so
405 * set, it can be read from, and written to.
406 *
407 * This function will return a pointer to a dentry if it succeeds. This
408 * pointer must be passed to the debugfs_remove() function when the file is
409 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700410 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700412 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700414 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * code.
416 */
Al Virof4ae40a2011-07-24 04:33:43 -0400417struct dentry *debugfs_create_u16(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 struct dentry *parent, u16 *value)
419{
Nicolai Stange4909f162016-03-22 14:11:17 +0100420 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
Stephen Boydb97f6792015-10-12 18:09:09 -0700421 &fops_u16_ro, &fops_u16_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423EXPORT_SYMBOL_GPL(debugfs_create_u16);
424
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800425static int debugfs_u32_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200426{
427 *(u32 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800428 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200429}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800430static int debugfs_u32_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200431{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800432 *val = *(u32 *)data;
433 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200434}
Nicolai Stange4909f162016-03-22 14:11:17 +0100435DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
436DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
437DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700440 * 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 -0700441 * @name: a pointer to a string containing the name of the file to create.
442 * @mode: the permission that the file should have
443 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700444 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * file will be created in the root of the debugfs filesystem.
446 * @value: a pointer to the variable that the file should read to and write
447 * from.
448 *
449 * This function creates a file in debugfs with the given name that
450 * contains the value of the variable @value. If the @mode variable is so
451 * set, it can be read from, and written to.
452 *
453 * This function will return a pointer to a dentry if it succeeds. This
454 * pointer must be passed to the debugfs_remove() function when the file is
455 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700456 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700458 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700460 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * code.
462 */
Al Virof4ae40a2011-07-24 04:33:43 -0400463struct dentry *debugfs_create_u32(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct dentry *parent, u32 *value)
465{
Nicolai Stange4909f162016-03-22 14:11:17 +0100466 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
Stephen Boydb97f6792015-10-12 18:09:09 -0700467 &fops_u32_ro, &fops_u32_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469EXPORT_SYMBOL_GPL(debugfs_create_u32);
470
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800471static int debugfs_u64_set(void *data, u64 val)
Michael Ellerman84478912007-04-17 15:59:36 +1000472{
473 *(u64 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800474 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000475}
476
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800477static int debugfs_u64_get(void *data, u64 *val)
Michael Ellerman84478912007-04-17 15:59:36 +1000478{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800479 *val = *(u64 *)data;
480 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000481}
Nicolai Stange4909f162016-03-22 14:11:17 +0100482DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
483DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
484DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
Michael Ellerman84478912007-04-17 15:59:36 +1000485
486/**
487 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
488 * @name: a pointer to a string containing the name of the file to create.
489 * @mode: the permission that the file should have
490 * @parent: a pointer to the parent dentry for this file. This should be a
491 * directory dentry if set. If this parameter is %NULL, then the
492 * file will be created in the root of the debugfs filesystem.
493 * @value: a pointer to the variable that the file should read to and write
494 * from.
495 *
496 * This function creates a file in debugfs with the given name that
497 * contains the value of the variable @value. If the @mode variable is so
498 * set, it can be read from, and written to.
499 *
500 * This function will return a pointer to a dentry if it succeeds. This
501 * pointer must be passed to the debugfs_remove() function when the file is
502 * to be removed (no automatic cleanup happens if your module is unloaded,
503 * you are responsible here.) If an error occurs, %NULL will be returned.
504 *
505 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
506 * returned. It is not wise to check for this value, but rather, check for
507 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
508 * code.
509 */
Al Virof4ae40a2011-07-24 04:33:43 -0400510struct dentry *debugfs_create_u64(const char *name, umode_t mode,
Michael Ellerman84478912007-04-17 15:59:36 +1000511 struct dentry *parent, u64 *value)
512{
Nicolai Stange4909f162016-03-22 14:11:17 +0100513 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
Stephen Boydb97f6792015-10-12 18:09:09 -0700514 &fops_u64_ro, &fops_u64_wo);
Michael Ellerman84478912007-04-17 15:59:36 +1000515}
516EXPORT_SYMBOL_GPL(debugfs_create_u64);
517
Viresh Kumarc23fe832015-10-18 22:43:19 +0530518static int debugfs_ulong_set(void *data, u64 val)
519{
520 *(unsigned long *)data = val;
521 return 0;
522}
523
524static int debugfs_ulong_get(void *data, u64 *val)
525{
526 *val = *(unsigned long *)data;
527 return 0;
528}
Nicolai Stange4909f162016-03-22 14:11:17 +0100529DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
530 "%llu\n");
531DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
532DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
Viresh Kumarc23fe832015-10-18 22:43:19 +0530533
534/**
535 * debugfs_create_ulong - create a debugfs file that is used to read and write
536 * an unsigned long value.
537 * @name: a pointer to a string containing the name of the file to create.
538 * @mode: the permission that the file should have
539 * @parent: a pointer to the parent dentry for this file. This should be a
540 * directory dentry if set. If this parameter is %NULL, then the
541 * file will be created in the root of the debugfs filesystem.
542 * @value: a pointer to the variable that the file should read to and write
543 * from.
544 *
545 * This function creates a file in debugfs with the given name that
546 * contains the value of the variable @value. If the @mode variable is so
547 * set, it can be read from, and written to.
548 *
549 * This function will return a pointer to a dentry if it succeeds. This
550 * pointer must be passed to the debugfs_remove() function when the file is
551 * to be removed (no automatic cleanup happens if your module is unloaded,
552 * you are responsible here.) If an error occurs, %NULL will be returned.
553 *
554 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
555 * returned. It is not wise to check for this value, but rather, check for
556 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
557 * code.
558 */
559struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
560 struct dentry *parent, unsigned long *value)
561{
Nicolai Stange4909f162016-03-22 14:11:17 +0100562 return debugfs_create_mode_unsafe(name, mode, parent, value,
563 &fops_ulong, &fops_ulong_ro,
564 &fops_ulong_wo);
Viresh Kumarc23fe832015-10-18 22:43:19 +0530565}
566EXPORT_SYMBOL_GPL(debugfs_create_ulong);
567
Nicolai Stange4909f162016-03-22 14:11:17 +0100568DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
569DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
570DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400571
Nicolai Stange4909f162016-03-22 14:11:17 +0100572DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
573 "0x%04llx\n");
574DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
575DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400576
Nicolai Stange4909f162016-03-22 14:11:17 +0100577DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
578 "0x%08llx\n");
579DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
580DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400581
Nicolai Stange4909f162016-03-22 14:11:17 +0100582DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
583 "0x%016llx\n");
584DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
585DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
Huang Ying15b0bea2010-05-18 14:35:23 +0800586
Randy Dunlape6716b82007-10-15 17:30:19 -0700587/*
Huang Ying15b0bea2010-05-18 14:35:23 +0800588 * 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 -0700589 *
590 * These functions are exactly the same as the above functions (but use a hex
591 * output for the decimal challenged). For details look at the above unsigned
592 * decimal functions.
593 */
594
Robin Getz2ebefc52007-08-02 18:23:50 -0400595/**
596 * 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 -0700597 * @name: a pointer to a string containing the name of the file to create.
598 * @mode: the permission that the file should have
599 * @parent: a pointer to the parent dentry for this file. This should be a
600 * directory dentry if set. If this parameter is %NULL, then the
601 * file will be created in the root of the debugfs filesystem.
602 * @value: a pointer to the variable that the file should read to and write
603 * from.
Robin Getz2ebefc52007-08-02 18:23:50 -0400604 */
Al Virof4ae40a2011-07-24 04:33:43 -0400605struct dentry *debugfs_create_x8(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400606 struct dentry *parent, u8 *value)
607{
Nicolai Stange4909f162016-03-22 14:11:17 +0100608 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
Stephen Boydb97f6792015-10-12 18:09:09 -0700609 &fops_x8_ro, &fops_x8_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400610}
611EXPORT_SYMBOL_GPL(debugfs_create_x8);
612
Randy Dunlape6716b82007-10-15 17:30:19 -0700613/**
614 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
615 * @name: a pointer to a string containing the name of the file to create.
616 * @mode: the permission that the file should have
617 * @parent: a pointer to the parent dentry for this file. This should be a
618 * directory dentry if set. If this parameter is %NULL, then the
619 * file will be created in the root of the debugfs filesystem.
620 * @value: a pointer to the variable that the file should read to and write
621 * from.
622 */
Al Virof4ae40a2011-07-24 04:33:43 -0400623struct dentry *debugfs_create_x16(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400624 struct dentry *parent, u16 *value)
625{
Nicolai Stange4909f162016-03-22 14:11:17 +0100626 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
Stephen Boydb97f6792015-10-12 18:09:09 -0700627 &fops_x16_ro, &fops_x16_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400628}
629EXPORT_SYMBOL_GPL(debugfs_create_x16);
630
Randy Dunlape6716b82007-10-15 17:30:19 -0700631/**
632 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
633 * @name: a pointer to a string containing the name of the file to create.
634 * @mode: the permission that the file should have
635 * @parent: a pointer to the parent dentry for this file. This should be a
636 * directory dentry if set. If this parameter is %NULL, then the
637 * file will be created in the root of the debugfs filesystem.
638 * @value: a pointer to the variable that the file should read to and write
639 * from.
640 */
Al Virof4ae40a2011-07-24 04:33:43 -0400641struct dentry *debugfs_create_x32(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400642 struct dentry *parent, u32 *value)
643{
Nicolai Stange4909f162016-03-22 14:11:17 +0100644 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
Stephen Boydb97f6792015-10-12 18:09:09 -0700645 &fops_x32_ro, &fops_x32_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400646}
647EXPORT_SYMBOL_GPL(debugfs_create_x32);
648
Huang Ying15b0bea2010-05-18 14:35:23 +0800649/**
650 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
651 * @name: a pointer to a string containing the name of the file to create.
652 * @mode: the permission that the file should have
653 * @parent: a pointer to the parent dentry for this file. This should be a
654 * directory dentry if set. If this parameter is %NULL, then the
655 * file will be created in the root of the debugfs filesystem.
656 * @value: a pointer to the variable that the file should read to and write
657 * from.
658 */
Al Virof4ae40a2011-07-24 04:33:43 -0400659struct dentry *debugfs_create_x64(const char *name, umode_t mode,
Huang Ying15b0bea2010-05-18 14:35:23 +0800660 struct dentry *parent, u64 *value)
661{
Nicolai Stange4909f162016-03-22 14:11:17 +0100662 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
Stephen Boyd82b7d4f2015-10-12 18:09:10 -0700663 &fops_x64_ro, &fops_x64_wo);
Huang Ying15b0bea2010-05-18 14:35:23 +0800664}
665EXPORT_SYMBOL_GPL(debugfs_create_x64);
666
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800667
668static int debugfs_size_t_set(void *data, u64 val)
669{
670 *(size_t *)data = val;
671 return 0;
672}
673static int debugfs_size_t_get(void *data, u64 *val)
674{
675 *val = *(size_t *)data;
676 return 0;
677}
Nicolai Stange4909f162016-03-22 14:11:17 +0100678DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
679 "%llu\n"); /* %llu and %zu are more or less the same */
680DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
681DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800682
683/**
684 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
685 * @name: a pointer to a string containing the name of the file to create.
686 * @mode: the permission that the file should have
687 * @parent: a pointer to the parent dentry for this file. This should be a
688 * directory dentry if set. If this parameter is %NULL, then the
689 * file will be created in the root of the debugfs filesystem.
690 * @value: a pointer to the variable that the file should read to and write
691 * from.
692 */
Al Virof4ae40a2011-07-24 04:33:43 -0400693struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800694 struct dentry *parent, size_t *value)
695{
Nicolai Stange4909f162016-03-22 14:11:17 +0100696 return debugfs_create_mode_unsafe(name, mode, parent, value,
697 &fops_size_t, &fops_size_t_ro,
698 &fops_size_t_wo);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800699}
700EXPORT_SYMBOL_GPL(debugfs_create_size_t);
701
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500702static int debugfs_atomic_t_set(void *data, u64 val)
703{
704 atomic_set((atomic_t *)data, val);
705 return 0;
706}
707static int debugfs_atomic_t_get(void *data, u64 *val)
708{
709 *val = atomic_read((atomic_t *)data);
710 return 0;
711}
Nicolai Stange4909f162016-03-22 14:11:17 +0100712DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500713 debugfs_atomic_t_set, "%lld\n");
Nicolai Stange4909f162016-03-22 14:11:17 +0100714DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
715 "%lld\n");
716DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
717 "%lld\n");
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500718
719/**
720 * debugfs_create_atomic_t - create a debugfs file that is used to read and
721 * write an atomic_t value
722 * @name: a pointer to a string containing the name of the file to create.
723 * @mode: the permission that the file should have
724 * @parent: a pointer to the parent dentry for this file. This should be a
725 * directory dentry if set. If this parameter is %NULL, then the
726 * file will be created in the root of the debugfs filesystem.
727 * @value: a pointer to the variable that the file should read to and write
728 * from.
729 */
730struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
731 struct dentry *parent, atomic_t *value)
732{
Nicolai Stange4909f162016-03-22 14:11:17 +0100733 return debugfs_create_mode_unsafe(name, mode, parent, value,
734 &fops_atomic_t, &fops_atomic_t_ro,
735 &fops_atomic_t_wo);
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500736}
737EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800738
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100739ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
740 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 char buf[3];
Nicolai Stange4d45f792016-03-22 14:11:18 +0100743 bool val;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100744 int r;
745 struct dentry *dentry = F_DENTRY(file);
Rahul Bedarkar88e412e2014-06-06 23:12:04 +0530746
Nicolai Stange69d29f92017-10-31 00:15:50 +0100747 r = debugfs_file_get(dentry);
748 if (unlikely(r))
Nicolai Stange4d45f792016-03-22 14:11:18 +0100749 return r;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100750 val = *(bool *)file->private_data;
751 debugfs_file_put(dentry);
Nicolai Stange4d45f792016-03-22 14:11:18 +0100752
753 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 buf[0] = 'Y';
755 else
756 buf[0] = 'N';
757 buf[1] = '\n';
758 buf[2] = 0x00;
759 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
760}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100761EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100763ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
764 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 char buf[32];
Stephen Boydc42d2232011-05-12 16:50:07 -0700767 size_t buf_size;
Jonathan Cameron8705b482011-04-19 12:43:46 +0100768 bool bv;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100769 int r;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700770 bool *val = file->private_data;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100771 struct dentry *dentry = F_DENTRY(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 buf_size = min(count, (sizeof(buf)-1));
774 if (copy_from_user(buf, user_buf, buf_size))
775 return -EFAULT;
776
Mathias Krausea3b2c8c72013-05-31 23:24:29 +0200777 buf[buf_size] = '\0';
Nicolai Stange4d45f792016-03-22 14:11:18 +0100778 if (strtobool(buf, &bv) == 0) {
Nicolai Stange69d29f92017-10-31 00:15:50 +0100779 r = debugfs_file_get(dentry);
780 if (unlikely(r))
Nicolai Stange4d45f792016-03-22 14:11:18 +0100781 return r;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100782 *val = bv;
783 debugfs_file_put(dentry);
Nicolai Stange4d45f792016-03-22 14:11:18 +0100784 }
Jonathan Cameron8705b482011-04-19 12:43:46 +0100785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return count;
787}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100788EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800790static const struct file_operations fops_bool = {
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100791 .read = debugfs_read_file_bool,
792 .write = debugfs_write_file_bool,
Stephen Boyd234e3402012-04-05 14:25:11 -0700793 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200794 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795};
796
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700797static const struct file_operations fops_bool_ro = {
798 .read = debugfs_read_file_bool,
799 .open = simple_open,
800 .llseek = default_llseek,
801};
802
803static const struct file_operations fops_bool_wo = {
804 .write = debugfs_write_file_bool,
805 .open = simple_open,
806 .llseek = default_llseek,
807};
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700810 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * @name: a pointer to a string containing the name of the file to create.
812 * @mode: the permission that the file should have
813 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700814 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * file will be created in the root of the debugfs filesystem.
816 * @value: a pointer to the variable that the file should read to and write
817 * from.
818 *
819 * This function creates a file in debugfs with the given name that
820 * contains the value of the variable @value. If the @mode variable is so
821 * set, it can be read from, and written to.
822 *
823 * This function will return a pointer to a dentry if it succeeds. This
824 * pointer must be passed to the debugfs_remove() function when the file is
825 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700826 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700828 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700830 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * code.
832 */
Al Virof4ae40a2011-07-24 04:33:43 -0400833struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700834 struct dentry *parent, bool *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Nicolai Stange4d45f792016-03-22 14:11:18 +0100836 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700837 &fops_bool_ro, &fops_bool_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839EXPORT_SYMBOL_GPL(debugfs_create_bool);
840
Michael Ellermandd308bc2006-03-07 21:41:59 +1100841static ssize_t read_file_blob(struct file *file, char __user *user_buf,
842 size_t count, loff_t *ppos)
843{
844 struct debugfs_blob_wrapper *blob = file->private_data;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100845 struct dentry *dentry = F_DENTRY(file);
Nicolai Stange83b711c2016-03-22 14:11:19 +0100846 ssize_t r;
Nicolai Stange83b711c2016-03-22 14:11:19 +0100847
Nicolai Stange69d29f92017-10-31 00:15:50 +0100848 r = debugfs_file_get(dentry);
849 if (unlikely(r))
850 return r;
851 r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
852 blob->size);
853 debugfs_file_put(dentry);
Nicolai Stange83b711c2016-03-22 14:11:19 +0100854 return r;
Michael Ellermandd308bc2006-03-07 21:41:59 +1100855}
856
Arjan van de Ven00977a52007-02-12 00:55:34 -0800857static const struct file_operations fops_blob = {
Michael Ellermandd308bc2006-03-07 21:41:59 +1100858 .read = read_file_blob,
Stephen Boyd234e3402012-04-05 14:25:11 -0700859 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200860 .llseek = default_llseek,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100861};
862
863/**
Jonathan Corbet400ced62009-05-25 10:15:27 -0600864 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
Michael Ellermandd308bc2006-03-07 21:41:59 +1100865 * @name: a pointer to a string containing the name of the file to create.
866 * @mode: the permission that the file should have
867 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700868 * directory dentry if set. If this parameter is %NULL, then the
Michael Ellermandd308bc2006-03-07 21:41:59 +1100869 * file will be created in the root of the debugfs filesystem.
870 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
871 * to the blob data and the size of the data.
872 *
873 * This function creates a file in debugfs with the given name that exports
874 * @blob->data as a binary blob. If the @mode variable is so set it can be
875 * read from. Writing is not supported.
876 *
877 * This function will return a pointer to a dentry if it succeeds. This
878 * pointer must be passed to the debugfs_remove() function when the file is
879 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700880 * you are responsible here.) If an error occurs, %NULL will be returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +1100881 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700882 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Michael Ellermandd308bc2006-03-07 21:41:59 +1100883 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700884 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Michael Ellermandd308bc2006-03-07 21:41:59 +1100885 * code.
886 */
Al Virof4ae40a2011-07-24 04:33:43 -0400887struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100888 struct dentry *parent,
889 struct debugfs_blob_wrapper *blob)
890{
Nicolai Stange83b711c2016-03-22 14:11:19 +0100891 return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
Michael Ellermandd308bc2006-03-07 21:41:59 +1100892}
893EXPORT_SYMBOL_GPL(debugfs_create_blob);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100894
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530895struct array_data {
896 void *array;
897 u32 elements;
898};
899
Linus Torvaldse05e2792012-09-21 11:48:05 -0700900static size_t u32_format_array(char *buf, size_t bufsize,
901 u32 *array, int array_size)
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530902{
903 size_t ret = 0;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530904
Linus Torvaldse05e2792012-09-21 11:48:05 -0700905 while (--array_size >= 0) {
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530906 size_t len;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700907 char term = array_size ? ' ' : '\n';
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530908
Linus Torvaldse05e2792012-09-21 11:48:05 -0700909 len = snprintf(buf, bufsize, "%u%c", *array++, term);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530910 ret += len;
911
Linus Torvaldse05e2792012-09-21 11:48:05 -0700912 buf += len;
913 bufsize -= len;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530914 }
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530915 return ret;
916}
917
David Rientjes36048852012-09-21 02:16:29 -0700918static int u32_array_open(struct inode *inode, struct file *file)
919{
920 struct array_data *data = inode->i_private;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700921 int size, elements = data->elements;
922 char *buf;
David Rientjes36048852012-09-21 02:16:29 -0700923
Linus Torvaldse05e2792012-09-21 11:48:05 -0700924 /*
925 * Max size:
926 * - 10 digits + ' '/'\n' = 11 bytes per number
927 * - terminating NUL character
928 */
929 size = elements*11;
930 buf = kmalloc(size+1, GFP_KERNEL);
931 if (!buf)
David Rientjes36048852012-09-21 02:16:29 -0700932 return -ENOMEM;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700933 buf[size] = 0;
934
935 file->private_data = buf;
936 u32_format_array(buf, size, data->array, data->elements);
937
David Rientjes36048852012-09-21 02:16:29 -0700938 return nonseekable_open(inode, file);
939}
940
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530941static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
942 loff_t *ppos)
943{
David Rientjes36048852012-09-21 02:16:29 -0700944 size_t size = strlen(file->private_data);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530945
946 return simple_read_from_buffer(buf, len, ppos,
947 file->private_data, size);
948}
949
950static int u32_array_release(struct inode *inode, struct file *file)
951{
952 kfree(file->private_data);
953
954 return 0;
955}
956
957static const struct file_operations u32_array_fops = {
958 .owner = THIS_MODULE,
959 .open = u32_array_open,
960 .release = u32_array_release,
961 .read = u32_array_read,
962 .llseek = no_llseek,
963};
964
965/**
966 * debugfs_create_u32_array - create a debugfs file that is used to read u32
967 * array.
968 * @name: a pointer to a string containing the name of the file to create.
969 * @mode: the permission that the file should have.
970 * @parent: a pointer to the parent dentry for this file. This should be a
971 * directory dentry if set. If this parameter is %NULL, then the
972 * file will be created in the root of the debugfs filesystem.
973 * @array: u32 array that provides data.
974 * @elements: total number of elements in the array.
975 *
976 * This function creates a file in debugfs with the given name that exports
977 * @array as data. If the @mode variable is so set it can be read from.
978 * Writing is not supported. Seek within the file is also not supported.
979 * Once array is created its size can not be changed.
980 *
981 * The function returns a pointer to dentry on success. If debugfs is not
982 * enabled in the kernel, the value -%ENODEV will be returned.
983 */
984struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
985 struct dentry *parent,
986 u32 *array, u32 elements)
987{
988 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
989
990 if (data == NULL)
991 return NULL;
992
993 data->array = array;
994 data->elements = elements;
995
Nicolai Stangec4a74f62016-03-22 14:11:20 +0100996 return debugfs_create_file_unsafe(name, mode, parent, data,
997 &u32_array_fops);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530998}
999EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1000
Heiko Carstens3b85e4a2011-12-27 15:08:28 +01001001#ifdef CONFIG_HAS_IOMEM
1002
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001003/*
1004 * The regset32 stuff is used to print 32-bit registers using the
1005 * seq_file utilities. We offer printing a register set in an already-opened
1006 * sequential file or create a debugfs file that only prints a regset32.
1007 */
1008
1009/**
1010 * debugfs_print_regs32 - use seq_print to describe a set of registers
1011 * @s: the seq_file structure being used to generate output
1012 * @regs: an array if struct debugfs_reg32 structures
Randy Dunlapb5763ac2012-01-21 11:02:42 -08001013 * @nregs: the length of the above array
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001014 * @base: the base address to be used in reading the registers
1015 * @prefix: a string to be prefixed to every output line
1016 *
1017 * This function outputs a text block describing the current values of
1018 * some 32-bit hardware registers. It is meant to be used within debugfs
1019 * files based on seq_file that need to show registers, intermixed with other
1020 * information. The prefix argument may be used to specify a leading string,
1021 * because some peripherals have several blocks of identical registers,
1022 * for example configuration of dma channels
1023 */
Joe Perches97615362014-09-29 16:08:26 -07001024void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1025 int nregs, void __iomem *base, char *prefix)
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001026{
Joe Perches97615362014-09-29 16:08:26 -07001027 int i;
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001028
1029 for (i = 0; i < nregs; i++, regs++) {
1030 if (prefix)
Joe Perches97615362014-09-29 16:08:26 -07001031 seq_printf(s, "%s", prefix);
1032 seq_printf(s, "%s = 0x%08x\n", regs->name,
1033 readl(base + regs->offset));
1034 if (seq_has_overflowed(s))
1035 break;
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001036 }
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001037}
1038EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1039
1040static int debugfs_show_regset32(struct seq_file *s, void *data)
1041{
1042 struct debugfs_regset32 *regset = s->private;
1043
1044 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1045 return 0;
1046}
1047
1048static int debugfs_open_regset32(struct inode *inode, struct file *file)
1049{
1050 return single_open(file, debugfs_show_regset32, inode->i_private);
1051}
1052
1053static const struct file_operations fops_regset32 = {
1054 .open = debugfs_open_regset32,
1055 .read = seq_read,
1056 .llseek = seq_lseek,
1057 .release = single_release,
1058};
1059
1060/**
1061 * debugfs_create_regset32 - create a debugfs file that returns register values
1062 * @name: a pointer to a string containing the name of the file to create.
1063 * @mode: the permission that the file should have
1064 * @parent: a pointer to the parent dentry for this file. This should be a
1065 * directory dentry if set. If this parameter is %NULL, then the
1066 * file will be created in the root of the debugfs filesystem.
1067 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1068 * to an array of register definitions, the array size and the base
1069 * address where the register bank is to be found.
1070 *
1071 * This function creates a file in debugfs with the given name that reports
1072 * the names and values of a set of 32-bit registers. If the @mode variable
1073 * is so set it can be read from. Writing is not supported.
1074 *
1075 * This function will return a pointer to a dentry if it succeeds. This
1076 * pointer must be passed to the debugfs_remove() function when the file is
1077 * to be removed (no automatic cleanup happens if your module is unloaded,
1078 * you are responsible here.) If an error occurs, %NULL will be returned.
1079 *
1080 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1081 * returned. It is not wise to check for this value, but rather, check for
1082 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1083 * code.
1084 */
Al Viro88187392012-03-20 06:00:24 -04001085struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001086 struct dentry *parent,
1087 struct debugfs_regset32 *regset)
1088{
1089 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
1090}
1091EXPORT_SYMBOL_GPL(debugfs_create_regset32);
Heiko Carstens3b85e4a2011-12-27 15:08:28 +01001092
1093#endif /* CONFIG_HAS_IOMEM */
Arend van Spriel98210b72014-11-09 11:31:58 +01001094
1095struct debugfs_devm_entry {
1096 int (*read)(struct seq_file *seq, void *data);
1097 struct device *dev;
1098};
1099
1100static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1101{
1102 struct debugfs_devm_entry *entry = inode->i_private;
1103
1104 return single_open(f, entry->read, entry->dev);
1105}
1106
1107static const struct file_operations debugfs_devm_entry_ops = {
1108 .owner = THIS_MODULE,
1109 .open = debugfs_devm_entry_open,
1110 .release = single_release,
1111 .read = seq_read,
1112 .llseek = seq_lseek
1113};
1114
1115/**
1116 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1117 *
1118 * @dev: device related to this debugfs file.
1119 * @name: name of the debugfs file.
1120 * @parent: a pointer to the parent dentry for this file. This should be a
1121 * directory dentry if set. If this parameter is %NULL, then the
1122 * file will be created in the root of the debugfs filesystem.
1123 * @read_fn: function pointer called to print the seq_file content.
1124 */
1125struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
1126 struct dentry *parent,
1127 int (*read_fn)(struct seq_file *s,
1128 void *data))
1129{
1130 struct debugfs_devm_entry *entry;
1131
1132 if (IS_ERR(parent))
1133 return ERR_PTR(-ENOENT);
1134
1135 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1136 if (!entry)
1137 return ERR_PTR(-ENOMEM);
1138
1139 entry->read = read_fn;
1140 entry->dev = dev;
1141
1142 return debugfs_create_file(name, S_IRUGO, parent, entry,
1143 &debugfs_devm_entry_ops);
1144}
1145EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1146