blob: 634b09d18b77f46f5a24f0cd0f75ab8fae0e7876 [file] [log] [blame]
Greg Kroah-Hartman3bce94fd2017-11-07 16:59:23 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * file.c - part of debugfs, a tiny little debug file system
4 *
5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2004 IBM Inc.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * debugfs is for people to use instead of /proc or /sys.
Mauro Carvalho Chehabe1b4fc72017-05-14 12:04:55 -03009 * See Documentation/filesystems/ for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/fs.h>
Alessandro Rubini1a087c62011-11-18 14:50:21 +010014#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pagemap.h>
16#include <linux/debugfs.h>
Alessandro Rubini03e099f2011-11-21 10:01:40 +010017#include <linux/io.h>
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +053018#include <linux/slab.h>
Seth Jennings3a76e5e2013-06-03 15:33:02 -050019#include <linux/atomic.h>
Arend van Spriel98210b72014-11-09 11:31:58 +010020#include <linux/device.h>
Al Virocfe39442018-02-01 12:14:57 -050021#include <linux/poll.h>
David Howells54961972019-08-19 17:18:02 -070022#include <linux/security.h>
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010023
24#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Nicolai Stange49d200d2016-03-22 14:11:14 +010026struct poll_table_struct;
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static ssize_t default_read_file(struct file *file, char __user *buf,
29 size_t count, loff_t *ppos)
30{
31 return 0;
32}
33
34static ssize_t default_write_file(struct file *file, const char __user *buf,
35 size_t count, loff_t *ppos)
36{
37 return count;
38}
39
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010040const struct file_operations debugfs_noop_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 .read = default_read_file,
42 .write = default_write_file,
Stephen Boyd234e3402012-04-05 14:25:11 -070043 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020044 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010047#define F_DENTRY(filp) ((filp)->f_path.dentry)
48
Nicolai Stange7c8d4692017-10-31 00:15:47 +010049const struct file_operations *debugfs_real_fops(const struct file *filp)
Nicolai Stange7c8d4692017-10-31 00:15:47 +010050{
51 struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
Nicolai Stange055ab8e2017-10-31 00:15:49 +010052
Nicolai Stange7d39bc52017-10-31 00:15:54 +010053 if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
54 /*
55 * Urgh, we've been called w/o a protecting
56 * debugfs_file_get().
57 */
58 WARN_ON(1);
59 return NULL;
60 }
61
Nicolai Stange7c8d4692017-10-31 00:15:47 +010062 return fsd->real_fops;
63}
64EXPORT_SYMBOL_GPL(debugfs_real_fops);
65
Nicolai Stangee9117a52017-10-31 00:15:48 +010066/**
67 * debugfs_file_get - mark the beginning of file data access
68 * @dentry: the dentry object whose data is being accessed.
69 *
70 * Up to a matching call to debugfs_file_put(), any successive call
71 * into the file removing functions debugfs_remove() and
72 * debugfs_remove_recursive() will block. Since associated private
73 * file data may only get freed after a successful return of any of
74 * the removal functions, you may safely access it after a successful
75 * call to debugfs_file_get() without worrying about lifetime issues.
76 *
77 * If -%EIO is returned, the file has already been removed and thus,
78 * it is not safe to access any of its data. If, on the other hand,
79 * it is allowed to access the file data, zero is returned.
80 */
81int debugfs_file_get(struct dentry *dentry)
82{
Nicolai Stange7d39bc52017-10-31 00:15:54 +010083 struct debugfs_fsdata *fsd;
84 void *d_fsd;
Nicolai Stangee9117a52017-10-31 00:15:48 +010085
Nicolai Stange7d39bc52017-10-31 00:15:54 +010086 d_fsd = READ_ONCE(dentry->d_fsdata);
87 if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
88 fsd = d_fsd;
89 } else {
90 fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
91 if (!fsd)
92 return -ENOMEM;
93
94 fsd->real_fops = (void *)((unsigned long)d_fsd &
95 ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
96 refcount_set(&fsd->active_users, 1);
97 init_completion(&fsd->active_users_drained);
98 if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
99 kfree(fsd);
100 fsd = READ_ONCE(dentry->d_fsdata);
101 }
102 }
103
104 /*
105 * In case of a successful cmpxchg() above, this check is
106 * strictly necessary and must follow it, see the comment in
107 * __debugfs_remove_file().
108 * OTOH, if the cmpxchg() hasn't been executed or wasn't
109 * successful, this serves the purpose of not starving
110 * removers.
111 */
Nicolai Stangee9117a52017-10-31 00:15:48 +0100112 if (d_unlinked(dentry))
113 return -EIO;
114
115 if (!refcount_inc_not_zero(&fsd->active_users))
116 return -EIO;
117
118 return 0;
119}
120EXPORT_SYMBOL_GPL(debugfs_file_get);
121
122/**
123 * debugfs_file_put - mark the end of file data access
124 * @dentry: the dentry object formerly passed to
125 * debugfs_file_get().
126 *
127 * Allow any ongoing concurrent call into debugfs_remove() or
128 * debugfs_remove_recursive() blocked by a former call to
129 * debugfs_file_get() to proceed and return to its caller.
130 */
131void debugfs_file_put(struct dentry *dentry)
132{
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100133 struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
Nicolai Stangee9117a52017-10-31 00:15:48 +0100134
135 if (refcount_dec_and_test(&fsd->active_users))
136 complete(&fsd->active_users_drained);
137}
138EXPORT_SYMBOL_GPL(debugfs_file_put);
139
David Howells54961972019-08-19 17:18:02 -0700140/*
141 * Only permit access to world-readable files when the kernel is locked down.
142 * We also need to exclude any file that has ways to write or alter it as root
143 * can bypass the permissions check.
144 */
Eric Snowberga37f4952019-12-07 11:16:03 -0500145static int debugfs_locked_down(struct inode *inode,
146 struct file *filp,
147 const struct file_operations *real_fops)
David Howells54961972019-08-19 17:18:02 -0700148{
149 if ((inode->i_mode & 07777) == 0444 &&
150 !(filp->f_mode & FMODE_WRITE) &&
151 !real_fops->unlocked_ioctl &&
152 !real_fops->compat_ioctl &&
153 !real_fops->mmap)
Eric Snowberga37f4952019-12-07 11:16:03 -0500154 return 0;
David Howells54961972019-08-19 17:18:02 -0700155
Eric Snowberga37f4952019-12-07 11:16:03 -0500156 if (security_locked_down(LOCKDOWN_DEBUGFS))
157 return -EPERM;
158
159 return 0;
David Howells54961972019-08-19 17:18:02 -0700160}
161
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100162static int open_proxy_open(struct inode *inode, struct file *filp)
163{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100164 struct dentry *dentry = F_DENTRY(filp);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100165 const struct file_operations *real_fops = NULL;
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100166 int r;
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100167
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100168 r = debugfs_file_get(dentry);
169 if (r)
170 return r == -EIO ? -ENOENT : r;
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100171
Christian Lamparter86f0e062016-09-17 21:43:01 +0200172 real_fops = debugfs_real_fops(filp);
David Howells54961972019-08-19 17:18:02 -0700173
Eric Snowberga37f4952019-12-07 11:16:03 -0500174 r = debugfs_locked_down(inode, filp, real_fops);
David Howells54961972019-08-19 17:18:02 -0700175 if (r)
176 goto out;
177
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100178 real_fops = fops_get(real_fops);
179 if (!real_fops) {
180 /* Huh? Module did not clean up after itself at exit? */
181 WARN(1, "debugfs file owner did not clean up at exit: %pd",
182 dentry);
183 r = -ENXIO;
184 goto out;
185 }
186 replace_fops(filp, real_fops);
187
188 if (real_fops->open)
189 r = real_fops->open(inode, filp);
190
191out:
Nicolai Stange69d29f92017-10-31 00:15:50 +0100192 debugfs_file_put(dentry);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100193 return r;
194}
195
196const struct file_operations debugfs_open_proxy_file_operations = {
197 .open = open_proxy_open,
198};
199
Nicolai Stange49d200d2016-03-22 14:11:14 +0100200#define PROTO(args...) args
201#define ARGS(args...) args
202
203#define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
204static ret_type full_proxy_ ## name(proto) \
205{ \
Nicolai Stange69d29f92017-10-31 00:15:50 +0100206 struct dentry *dentry = F_DENTRY(filp); \
Nicolai Stange154b9d72017-10-31 00:15:53 +0100207 const struct file_operations *real_fops; \
Nicolai Stange49d200d2016-03-22 14:11:14 +0100208 ret_type r; \
209 \
Nicolai Stange69d29f92017-10-31 00:15:50 +0100210 r = debugfs_file_get(dentry); \
211 if (unlikely(r)) \
212 return r; \
Nicolai Stange154b9d72017-10-31 00:15:53 +0100213 real_fops = debugfs_real_fops(filp); \
Nicolai Stange69d29f92017-10-31 00:15:50 +0100214 r = real_fops->name(args); \
215 debugfs_file_put(dentry); \
Nicolai Stange49d200d2016-03-22 14:11:14 +0100216 return r; \
217}
218
219FULL_PROXY_FUNC(llseek, loff_t, filp,
220 PROTO(struct file *filp, loff_t offset, int whence),
221 ARGS(filp, offset, whence));
222
223FULL_PROXY_FUNC(read, ssize_t, filp,
224 PROTO(struct file *filp, char __user *buf, size_t size,
225 loff_t *ppos),
226 ARGS(filp, buf, size, ppos));
227
228FULL_PROXY_FUNC(write, ssize_t, filp,
229 PROTO(struct file *filp, const char __user *buf, size_t size,
230 loff_t *ppos),
231 ARGS(filp, buf, size, ppos));
232
233FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
234 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
235 ARGS(filp, cmd, arg));
236
Al Viro076ccb72017-07-03 01:02:18 -0400237static __poll_t full_proxy_poll(struct file *filp,
Nicolai Stange49d200d2016-03-22 14:11:14 +0100238 struct poll_table_struct *wait)
239{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100240 struct dentry *dentry = F_DENTRY(filp);
Al Viroe6c8adc2017-07-03 22:25:56 -0400241 __poll_t r = 0;
Nicolai Stange154b9d72017-10-31 00:15:53 +0100242 const struct file_operations *real_fops;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100243
Nicolai Stange69d29f92017-10-31 00:15:50 +0100244 if (debugfs_file_get(dentry))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800245 return EPOLLHUP;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100246
Nicolai Stange154b9d72017-10-31 00:15:53 +0100247 real_fops = debugfs_real_fops(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100248 r = real_fops->poll(filp, wait);
Nicolai Stange69d29f92017-10-31 00:15:50 +0100249 debugfs_file_put(dentry);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100250 return r;
251}
252
253static int full_proxy_release(struct inode *inode, struct file *filp)
254{
255 const struct dentry *dentry = F_DENTRY(filp);
Christian Lamparter86f0e062016-09-17 21:43:01 +0200256 const struct file_operations *real_fops = debugfs_real_fops(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100257 const struct file_operations *proxy_fops = filp->f_op;
258 int r = 0;
259
260 /*
261 * We must not protect this against removal races here: the
262 * original releaser should be called unconditionally in order
263 * not to leak any resources. Releasers must not assume that
264 * ->i_private is still being meaningful here.
265 */
266 if (real_fops->release)
267 r = real_fops->release(inode, filp);
268
269 replace_fops(filp, d_inode(dentry)->i_fop);
270 kfree((void *)proxy_fops);
271 fops_put(real_fops);
Eric Engestroma1a9e5d2016-09-21 10:27:36 +0100272 return r;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100273}
274
275static void __full_proxy_fops_init(struct file_operations *proxy_fops,
276 const struct file_operations *real_fops)
277{
278 proxy_fops->release = full_proxy_release;
279 if (real_fops->llseek)
280 proxy_fops->llseek = full_proxy_llseek;
281 if (real_fops->read)
282 proxy_fops->read = full_proxy_read;
283 if (real_fops->write)
284 proxy_fops->write = full_proxy_write;
285 if (real_fops->poll)
286 proxy_fops->poll = full_proxy_poll;
287 if (real_fops->unlocked_ioctl)
288 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
289}
290
291static int full_proxy_open(struct inode *inode, struct file *filp)
292{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100293 struct dentry *dentry = F_DENTRY(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100294 const struct file_operations *real_fops = NULL;
295 struct file_operations *proxy_fops = NULL;
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100296 int r;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100297
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100298 r = debugfs_file_get(dentry);
299 if (r)
300 return r == -EIO ? -ENOENT : r;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100301
Christian Lamparter86f0e062016-09-17 21:43:01 +0200302 real_fops = debugfs_real_fops(filp);
David Howells54961972019-08-19 17:18:02 -0700303
Eric Snowberga37f4952019-12-07 11:16:03 -0500304 r = debugfs_locked_down(inode, filp, real_fops);
David Howells54961972019-08-19 17:18:02 -0700305 if (r)
306 goto out;
307
Nicolai Stange49d200d2016-03-22 14:11:14 +0100308 real_fops = fops_get(real_fops);
309 if (!real_fops) {
310 /* Huh? Module did not cleanup after itself at exit? */
311 WARN(1, "debugfs file owner did not clean up at exit: %pd",
312 dentry);
313 r = -ENXIO;
314 goto out;
315 }
316
317 proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
318 if (!proxy_fops) {
319 r = -ENOMEM;
320 goto free_proxy;
321 }
322 __full_proxy_fops_init(proxy_fops, real_fops);
323 replace_fops(filp, proxy_fops);
324
325 if (real_fops->open) {
326 r = real_fops->open(inode, filp);
Nicolai Stangeb10e3e92016-05-24 13:08:53 +0200327 if (r) {
328 replace_fops(filp, d_inode(dentry)->i_fop);
329 goto free_proxy;
330 } else if (filp->f_op != proxy_fops) {
Nicolai Stange49d200d2016-03-22 14:11:14 +0100331 /* No protection against file removal anymore. */
332 WARN(1, "debugfs file owner replaced proxy fops: %pd",
333 dentry);
334 goto free_proxy;
335 }
336 }
337
338 goto out;
339free_proxy:
340 kfree(proxy_fops);
341 fops_put(real_fops);
342out:
Nicolai Stange69d29f92017-10-31 00:15:50 +0100343 debugfs_file_put(dentry);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100344 return r;
345}
346
347const struct file_operations debugfs_full_proxy_file_operations = {
348 .open = full_proxy_open,
349};
350
Nicolai Stangec6468802016-03-22 14:11:15 +0100351ssize_t debugfs_attr_read(struct file *file, char __user *buf,
352 size_t len, loff_t *ppos)
353{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100354 struct dentry *dentry = F_DENTRY(file);
Nicolai Stangec6468802016-03-22 14:11:15 +0100355 ssize_t ret;
Nicolai Stangec6468802016-03-22 14:11:15 +0100356
Nicolai Stange69d29f92017-10-31 00:15:50 +0100357 ret = debugfs_file_get(dentry);
358 if (unlikely(ret))
359 return ret;
360 ret = simple_attr_read(file, buf, len, ppos);
361 debugfs_file_put(dentry);
Nicolai Stangec6468802016-03-22 14:11:15 +0100362 return ret;
363}
364EXPORT_SYMBOL_GPL(debugfs_attr_read);
365
366ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
367 size_t len, loff_t *ppos)
368{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100369 struct dentry *dentry = F_DENTRY(file);
Nicolai Stangec6468802016-03-22 14:11:15 +0100370 ssize_t ret;
Nicolai Stangec6468802016-03-22 14:11:15 +0100371
Nicolai Stange69d29f92017-10-31 00:15:50 +0100372 ret = debugfs_file_get(dentry);
373 if (unlikely(ret))
374 return ret;
375 ret = simple_attr_write(file, buf, len, ppos);
376 debugfs_file_put(dentry);
Nicolai Stangec6468802016-03-22 14:11:15 +0100377 return ret;
378}
379EXPORT_SYMBOL_GPL(debugfs_attr_write);
380
Nicolai Stange4909f162016-03-22 14:11:17 +0100381static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
382 struct dentry *parent, void *value,
383 const struct file_operations *fops,
384 const struct file_operations *fops_ro,
385 const struct file_operations *fops_wo)
386{
387 /* if there are no write bits set, make read only */
388 if (!(mode & S_IWUGO))
389 return debugfs_create_file_unsafe(name, mode, parent, value,
390 fops_ro);
391 /* if there are no read bits set, make write only */
392 if (!(mode & S_IRUGO))
393 return debugfs_create_file_unsafe(name, mode, parent, value,
394 fops_wo);
395
396 return debugfs_create_file_unsafe(name, mode, parent, value, fops);
397}
398
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800399static int debugfs_u8_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200400{
401 *(u8 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800402 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200403}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800404static int debugfs_u8_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200405{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800406 *val = *(u8 *)data;
407 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200408}
Nicolai Stange4909f162016-03-22 14:11:17 +0100409DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
410DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
411DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700414 * 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 -0700415 * @name: a pointer to a string containing the name of the file to create.
416 * @mode: the permission that the file should have
417 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700418 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * file will be created in the root of the debugfs filesystem.
420 * @value: a pointer to the variable that the file should read to and write
421 * from.
422 *
423 * This function creates a file in debugfs with the given name that
424 * contains the value of the variable @value. If the @mode variable is so
425 * set, it can be read from, and written to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
Greg Kroah-Hartman9655ac42019-10-11 15:29:24 +0200427void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
428 u8 *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Greg Kroah-Hartman9655ac42019-10-11 15:29:24 +0200430 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
Stephen Boydb97f6792015-10-12 18:09:09 -0700431 &fops_u8_ro, &fops_u8_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433EXPORT_SYMBOL_GPL(debugfs_create_u8);
434
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800435static int debugfs_u16_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200436{
437 *(u16 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800438 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200439}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800440static int debugfs_u16_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200441{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800442 *val = *(u16 *)data;
443 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200444}
Nicolai Stange4909f162016-03-22 14:11:17 +0100445DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
446DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
447DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700450 * 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 -0700451 * @name: a pointer to a string containing the name of the file to create.
452 * @mode: the permission that the file should have
453 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700454 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * file will be created in the root of the debugfs filesystem.
456 * @value: a pointer to the variable that the file should read to and write
457 * from.
458 *
459 * This function creates a file in debugfs with the given name that
460 * contains the value of the variable @value. If the @mode variable is so
461 * set, it can be read from, and written to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Greg Kroah-Hartman313f5db2019-10-11 15:29:25 +0200463void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
464 u16 *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Greg Kroah-Hartman313f5db2019-10-11 15:29:25 +0200466 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
Stephen Boydb97f6792015-10-12 18:09:09 -0700467 &fops_u16_ro, &fops_u16_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469EXPORT_SYMBOL_GPL(debugfs_create_u16);
470
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800471static int debugfs_u32_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200472{
473 *(u32 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800474 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200475}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800476static int debugfs_u32_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200477{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800478 *val = *(u32 *)data;
479 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200480}
Nicolai Stange4909f162016-03-22 14:11:17 +0100481DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
482DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
483DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700486 * 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 -0700487 * @name: a pointer to a string containing the name of the file to create.
488 * @mode: the permission that the file should have
489 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700490 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * file will be created in the root of the debugfs filesystem.
492 * @value: a pointer to the variable that the file should read to and write
493 * from.
494 *
495 * This function creates a file in debugfs with the given name that
496 * contains the value of the variable @value. If the @mode variable is so
497 * set, it can be read from, and written to.
498 *
499 * This function will return a pointer to a dentry if it succeeds. This
500 * pointer must be passed to the debugfs_remove() function when the file is
501 * to be removed (no automatic cleanup happens if your module is unloaded,
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -0300502 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
Ronald Tschalär9abb2492019-04-15 01:25:05 -0700503 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -0300505 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
Ronald Tschalär9abb2492019-04-15 01:25:05 -0700506 * be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Al Virof4ae40a62011-07-24 04:33:43 -0400508struct dentry *debugfs_create_u32(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct dentry *parent, u32 *value)
510{
Nicolai Stange4909f162016-03-22 14:11:17 +0100511 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
Stephen Boydb97f6792015-10-12 18:09:09 -0700512 &fops_u32_ro, &fops_u32_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514EXPORT_SYMBOL_GPL(debugfs_create_u32);
515
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800516static int debugfs_u64_set(void *data, u64 val)
Michael Ellerman84478912007-04-17 15:59:36 +1000517{
518 *(u64 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800519 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000520}
521
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800522static int debugfs_u64_get(void *data, u64 *val)
Michael Ellerman84478912007-04-17 15:59:36 +1000523{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800524 *val = *(u64 *)data;
525 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000526}
Nicolai Stange4909f162016-03-22 14:11:17 +0100527DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
528DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
529DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
Michael Ellerman84478912007-04-17 15:59:36 +1000530
531/**
532 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
533 * @name: a pointer to a string containing the name of the file to create.
534 * @mode: the permission that the file should have
535 * @parent: a pointer to the parent dentry for this file. This should be a
536 * directory dentry if set. If this parameter is %NULL, then the
537 * file will be created in the root of the debugfs filesystem.
538 * @value: a pointer to the variable that the file should read to and write
539 * from.
540 *
541 * This function creates a file in debugfs with the given name that
542 * contains the value of the variable @value. If the @mode variable is so
543 * set, it can be read from, and written to.
Michael Ellerman84478912007-04-17 15:59:36 +1000544 */
Greg Kroah-Hartmanad262212019-10-11 15:29:26 +0200545void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
546 u64 *value)
Michael Ellerman84478912007-04-17 15:59:36 +1000547{
Greg Kroah-Hartmanad262212019-10-11 15:29:26 +0200548 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
Stephen Boydb97f6792015-10-12 18:09:09 -0700549 &fops_u64_ro, &fops_u64_wo);
Michael Ellerman84478912007-04-17 15:59:36 +1000550}
551EXPORT_SYMBOL_GPL(debugfs_create_u64);
552
Viresh Kumarc23fe832015-10-18 22:43:19 +0530553static int debugfs_ulong_set(void *data, u64 val)
554{
555 *(unsigned long *)data = val;
556 return 0;
557}
558
559static int debugfs_ulong_get(void *data, u64 *val)
560{
561 *val = *(unsigned long *)data;
562 return 0;
563}
Nicolai Stange4909f162016-03-22 14:11:17 +0100564DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
565 "%llu\n");
566DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
567DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
Viresh Kumarc23fe832015-10-18 22:43:19 +0530568
569/**
570 * debugfs_create_ulong - create a debugfs file that is used to read and write
571 * an unsigned long value.
572 * @name: a pointer to a string containing the name of the file to create.
573 * @mode: the permission that the file should have
574 * @parent: a pointer to the parent dentry for this file. This should be a
575 * directory dentry if set. If this parameter is %NULL, then the
576 * file will be created in the root of the debugfs filesystem.
577 * @value: a pointer to the variable that the file should read to and write
578 * from.
579 *
580 * This function creates a file in debugfs with the given name that
581 * contains the value of the variable @value. If the @mode variable is so
582 * set, it can be read from, and written to.
583 *
584 * This function will return a pointer to a dentry if it succeeds. This
585 * pointer must be passed to the debugfs_remove() function when the file is
586 * to be removed (no automatic cleanup happens if your module is unloaded,
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -0300587 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
Ronald Tschalär9abb2492019-04-15 01:25:05 -0700588 * returned.
Viresh Kumarc23fe832015-10-18 22:43:19 +0530589 *
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -0300590 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
Ronald Tschalär9abb2492019-04-15 01:25:05 -0700591 * be returned.
Viresh Kumarc23fe832015-10-18 22:43:19 +0530592 */
593struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
594 struct dentry *parent, unsigned long *value)
595{
Nicolai Stange4909f162016-03-22 14:11:17 +0100596 return debugfs_create_mode_unsafe(name, mode, parent, value,
597 &fops_ulong, &fops_ulong_ro,
598 &fops_ulong_wo);
Viresh Kumarc23fe832015-10-18 22:43:19 +0530599}
600EXPORT_SYMBOL_GPL(debugfs_create_ulong);
601
Nicolai Stange4909f162016-03-22 14:11:17 +0100602DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
603DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
604DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400605
Nicolai Stange4909f162016-03-22 14:11:17 +0100606DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
607 "0x%04llx\n");
608DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
609DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400610
Nicolai Stange4909f162016-03-22 14:11:17 +0100611DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
612 "0x%08llx\n");
613DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
614DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400615
Nicolai Stange4909f162016-03-22 14:11:17 +0100616DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
617 "0x%016llx\n");
618DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
619DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
Huang Ying15b0bea2010-05-18 14:35:23 +0800620
Randy Dunlape6716b82007-10-15 17:30:19 -0700621/*
Huang Ying15b0bea2010-05-18 14:35:23 +0800622 * 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 -0700623 *
624 * These functions are exactly the same as the above functions (but use a hex
625 * output for the decimal challenged). For details look at the above unsigned
626 * decimal functions.
627 */
628
Robin Getz2ebefc52007-08-02 18:23:50 -0400629/**
630 * 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 -0700631 * @name: a pointer to a string containing the name of the file to create.
632 * @mode: the permission that the file should have
633 * @parent: a pointer to the parent dentry for this file. This should be a
634 * directory dentry if set. If this parameter is %NULL, then the
635 * file will be created in the root of the debugfs filesystem.
636 * @value: a pointer to the variable that the file should read to and write
637 * from.
Robin Getz2ebefc52007-08-02 18:23:50 -0400638 */
Greg Kroah-Hartmanc7c11682019-10-11 15:29:28 +0200639void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
640 u8 *value)
Robin Getz2ebefc52007-08-02 18:23:50 -0400641{
Greg Kroah-Hartmanc7c11682019-10-11 15:29:28 +0200642 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
Stephen Boydb97f6792015-10-12 18:09:09 -0700643 &fops_x8_ro, &fops_x8_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400644}
645EXPORT_SYMBOL_GPL(debugfs_create_x8);
646
Randy Dunlape6716b82007-10-15 17:30:19 -0700647/**
648 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
649 * @name: a pointer to a string containing the name of the file to create.
650 * @mode: the permission that the file should have
651 * @parent: a pointer to the parent dentry for this file. This should be a
652 * directory dentry if set. If this parameter is %NULL, then the
653 * file will be created in the root of the debugfs filesystem.
654 * @value: a pointer to the variable that the file should read to and write
655 * from.
656 */
Greg Kroah-Hartmane40d38f2019-10-11 15:29:29 +0200657void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
658 u16 *value)
Robin Getz2ebefc52007-08-02 18:23:50 -0400659{
Greg Kroah-Hartmane40d38f2019-10-11 15:29:29 +0200660 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
Stephen Boydb97f6792015-10-12 18:09:09 -0700661 &fops_x16_ro, &fops_x16_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400662}
663EXPORT_SYMBOL_GPL(debugfs_create_x16);
664
Randy Dunlape6716b82007-10-15 17:30:19 -0700665/**
666 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
667 * @name: a pointer to a string containing the name of the file to create.
668 * @mode: the permission that the file should have
669 * @parent: a pointer to the parent dentry for this file. This should be a
670 * directory dentry if set. If this parameter is %NULL, then the
671 * file will be created in the root of the debugfs filesystem.
672 * @value: a pointer to the variable that the file should read to and write
673 * from.
674 */
Greg Kroah-Hartmanf5cb0a72019-10-11 15:29:30 +0200675void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
676 u32 *value)
Robin Getz2ebefc52007-08-02 18:23:50 -0400677{
Greg Kroah-Hartmanf5cb0a72019-10-11 15:29:30 +0200678 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
Stephen Boydb97f6792015-10-12 18:09:09 -0700679 &fops_x32_ro, &fops_x32_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400680}
681EXPORT_SYMBOL_GPL(debugfs_create_x32);
682
Huang Ying15b0bea2010-05-18 14:35:23 +0800683/**
684 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit 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 */
Greg Kroah-Hartman0864c402019-10-11 15:29:31 +0200693void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
694 u64 *value)
Huang Ying15b0bea2010-05-18 14:35:23 +0800695{
Greg Kroah-Hartman0864c402019-10-11 15:29:31 +0200696 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
Stephen Boyd82b7d4f2015-10-12 18:09:10 -0700697 &fops_x64_ro, &fops_x64_wo);
Huang Ying15b0bea2010-05-18 14:35:23 +0800698}
699EXPORT_SYMBOL_GPL(debugfs_create_x64);
700
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800701
702static int debugfs_size_t_set(void *data, u64 val)
703{
704 *(size_t *)data = val;
705 return 0;
706}
707static int debugfs_size_t_get(void *data, u64 *val)
708{
709 *val = *(size_t *)data;
710 return 0;
711}
Nicolai Stange4909f162016-03-22 14:11:17 +0100712DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
713 "%llu\n"); /* %llu and %zu are more or less the same */
714DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
715DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800716
717/**
718 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
719 * @name: a pointer to a string containing the name of the file to create.
720 * @mode: the permission that the file should have
721 * @parent: a pointer to the parent dentry for this file. This should be a
722 * directory dentry if set. If this parameter is %NULL, then the
723 * file will be created in the root of the debugfs filesystem.
724 * @value: a pointer to the variable that the file should read to and write
725 * from.
726 */
Greg Kroah-Hartman8e580262019-10-11 15:29:27 +0200727void debugfs_create_size_t(const char *name, umode_t mode,
728 struct dentry *parent, size_t *value)
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800729{
Greg Kroah-Hartman8e580262019-10-11 15:29:27 +0200730 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
731 &fops_size_t_ro, &fops_size_t_wo);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800732}
733EXPORT_SYMBOL_GPL(debugfs_create_size_t);
734
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500735static int debugfs_atomic_t_set(void *data, u64 val)
736{
737 atomic_set((atomic_t *)data, val);
738 return 0;
739}
740static int debugfs_atomic_t_get(void *data, u64 *val)
741{
742 *val = atomic_read((atomic_t *)data);
743 return 0;
744}
Nicolai Stange4909f162016-03-22 14:11:17 +0100745DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500746 debugfs_atomic_t_set, "%lld\n");
Nicolai Stange4909f162016-03-22 14:11:17 +0100747DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
748 "%lld\n");
749DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
750 "%lld\n");
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500751
752/**
753 * debugfs_create_atomic_t - create a debugfs file that is used to read and
754 * write an atomic_t value
755 * @name: a pointer to a string containing the name of the file to create.
756 * @mode: the permission that the file should have
757 * @parent: a pointer to the parent dentry for this file. This should be a
758 * directory dentry if set. If this parameter is %NULL, then the
759 * file will be created in the root of the debugfs filesystem.
760 * @value: a pointer to the variable that the file should read to and write
761 * from.
762 */
Greg Kroah-Hartman9927c6f2019-10-16 06:03:32 -0700763void debugfs_create_atomic_t(const char *name, umode_t mode,
764 struct dentry *parent, atomic_t *value)
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500765{
Greg Kroah-Hartman9927c6f2019-10-16 06:03:32 -0700766 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
767 &fops_atomic_t_ro, &fops_atomic_t_wo);
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500768}
769EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800770
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100771ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
772 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 char buf[3];
Nicolai Stange4d45f792016-03-22 14:11:18 +0100775 bool val;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100776 int r;
777 struct dentry *dentry = F_DENTRY(file);
Rahul Bedarkar88e412e2014-06-06 23:12:04 +0530778
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 = *(bool *)file->private_data;
783 debugfs_file_put(dentry);
Nicolai Stange4d45f792016-03-22 14:11:18 +0100784
785 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 buf[0] = 'Y';
787 else
788 buf[0] = 'N';
789 buf[1] = '\n';
790 buf[2] = 0x00;
791 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
792}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100793EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100795ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
796 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Jonathan Cameron8705b482011-04-19 12:43:46 +0100798 bool bv;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100799 int r;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700800 bool *val = file->private_data;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100801 struct dentry *dentry = F_DENTRY(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Andy Shevchenko964f8362018-05-03 19:17:52 +0300803 r = kstrtobool_from_user(user_buf, count, &bv);
804 if (!r) {
Nicolai Stange69d29f92017-10-31 00:15:50 +0100805 r = debugfs_file_get(dentry);
806 if (unlikely(r))
Nicolai Stange4d45f792016-03-22 14:11:18 +0100807 return r;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100808 *val = bv;
809 debugfs_file_put(dentry);
Nicolai Stange4d45f792016-03-22 14:11:18 +0100810 }
Jonathan Cameron8705b482011-04-19 12:43:46 +0100811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return count;
813}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100814EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800816static const struct file_operations fops_bool = {
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100817 .read = debugfs_read_file_bool,
818 .write = debugfs_write_file_bool,
Stephen Boyd234e3402012-04-05 14:25:11 -0700819 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200820 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821};
822
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700823static const struct file_operations fops_bool_ro = {
824 .read = debugfs_read_file_bool,
825 .open = simple_open,
826 .llseek = default_llseek,
827};
828
829static const struct file_operations fops_bool_wo = {
830 .write = debugfs_write_file_bool,
831 .open = simple_open,
832 .llseek = default_llseek,
833};
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700836 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 * @name: a pointer to a string containing the name of the file to create.
838 * @mode: the permission that the file should have
839 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700840 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * file will be created in the root of the debugfs filesystem.
842 * @value: a pointer to the variable that the file should read to and write
843 * from.
844 *
845 * This function creates a file in debugfs with the given name that
846 * contains the value of the variable @value. If the @mode variable is so
847 * set, it can be read from, and written to.
848 *
849 * This function will return a pointer to a dentry if it succeeds. This
850 * pointer must be passed to the debugfs_remove() function when the file is
851 * to be removed (no automatic cleanup happens if your module is unloaded,
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -0300852 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
Ronald Tschalär9abb2492019-04-15 01:25:05 -0700853 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 *
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -0300855 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
Ronald Tschalär9abb2492019-04-15 01:25:05 -0700856 * be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 */
Al Virof4ae40a62011-07-24 04:33:43 -0400858struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700859 struct dentry *parent, bool *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Nicolai Stange4d45f792016-03-22 14:11:18 +0100861 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700862 &fops_bool_ro, &fops_bool_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864EXPORT_SYMBOL_GPL(debugfs_create_bool);
865
Michael Ellermandd308bc2006-03-07 21:41:59 +1100866static ssize_t read_file_blob(struct file *file, char __user *user_buf,
867 size_t count, loff_t *ppos)
868{
869 struct debugfs_blob_wrapper *blob = file->private_data;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100870 struct dentry *dentry = F_DENTRY(file);
Nicolai Stange83b711cb2016-03-22 14:11:19 +0100871 ssize_t r;
Nicolai Stange83b711cb2016-03-22 14:11:19 +0100872
Nicolai Stange69d29f92017-10-31 00:15:50 +0100873 r = debugfs_file_get(dentry);
874 if (unlikely(r))
875 return r;
876 r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
877 blob->size);
878 debugfs_file_put(dentry);
Nicolai Stange83b711cb2016-03-22 14:11:19 +0100879 return r;
Michael Ellermandd308bc2006-03-07 21:41:59 +1100880}
881
Arjan van de Ven00977a52007-02-12 00:55:34 -0800882static const struct file_operations fops_blob = {
Michael Ellermandd308bc2006-03-07 21:41:59 +1100883 .read = read_file_blob,
Stephen Boyd234e3402012-04-05 14:25:11 -0700884 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200885 .llseek = default_llseek,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100886};
887
888/**
Jonathan Corbet400ced62009-05-25 10:15:27 -0600889 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
Michael Ellermandd308bc2006-03-07 21:41:59 +1100890 * @name: a pointer to a string containing the name of the file to create.
891 * @mode: the permission that the file should have
892 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700893 * directory dentry if set. If this parameter is %NULL, then the
Michael Ellermandd308bc2006-03-07 21:41:59 +1100894 * file will be created in the root of the debugfs filesystem.
895 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
896 * to the blob data and the size of the data.
897 *
898 * This function creates a file in debugfs with the given name that exports
899 * @blob->data as a binary blob. If the @mode variable is so set it can be
900 * read from. Writing is not supported.
901 *
902 * This function will return a pointer to a dentry if it succeeds. This
903 * pointer must be passed to the debugfs_remove() function when the file is
904 * to be removed (no automatic cleanup happens if your module is unloaded,
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -0300905 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
Ronald Tschalär9abb2492019-04-15 01:25:05 -0700906 * returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +1100907 *
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -0300908 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
Ronald Tschalär9abb2492019-04-15 01:25:05 -0700909 * be returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +1100910 */
Al Virof4ae40a62011-07-24 04:33:43 -0400911struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100912 struct dentry *parent,
913 struct debugfs_blob_wrapper *blob)
914{
Nicolai Stange83b711cb2016-03-22 14:11:19 +0100915 return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
Michael Ellermandd308bc2006-03-07 21:41:59 +1100916}
917EXPORT_SYMBOL_GPL(debugfs_create_blob);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100918
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530919struct array_data {
920 void *array;
921 u32 elements;
922};
923
Linus Torvaldse05e2792012-09-21 11:48:05 -0700924static size_t u32_format_array(char *buf, size_t bufsize,
925 u32 *array, int array_size)
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530926{
927 size_t ret = 0;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530928
Linus Torvaldse05e2792012-09-21 11:48:05 -0700929 while (--array_size >= 0) {
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530930 size_t len;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700931 char term = array_size ? ' ' : '\n';
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530932
Linus Torvaldse05e2792012-09-21 11:48:05 -0700933 len = snprintf(buf, bufsize, "%u%c", *array++, term);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530934 ret += len;
935
Linus Torvaldse05e2792012-09-21 11:48:05 -0700936 buf += len;
937 bufsize -= len;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530938 }
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530939 return ret;
940}
941
David Rientjes36048852012-09-21 02:16:29 -0700942static int u32_array_open(struct inode *inode, struct file *file)
943{
944 struct array_data *data = inode->i_private;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700945 int size, elements = data->elements;
946 char *buf;
David Rientjes36048852012-09-21 02:16:29 -0700947
Linus Torvaldse05e2792012-09-21 11:48:05 -0700948 /*
949 * Max size:
950 * - 10 digits + ' '/'\n' = 11 bytes per number
951 * - terminating NUL character
952 */
953 size = elements*11;
954 buf = kmalloc(size+1, GFP_KERNEL);
955 if (!buf)
David Rientjes36048852012-09-21 02:16:29 -0700956 return -ENOMEM;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700957 buf[size] = 0;
958
959 file->private_data = buf;
960 u32_format_array(buf, size, data->array, data->elements);
961
David Rientjes36048852012-09-21 02:16:29 -0700962 return nonseekable_open(inode, file);
963}
964
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530965static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
966 loff_t *ppos)
967{
David Rientjes36048852012-09-21 02:16:29 -0700968 size_t size = strlen(file->private_data);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530969
970 return simple_read_from_buffer(buf, len, ppos,
971 file->private_data, size);
972}
973
974static int u32_array_release(struct inode *inode, struct file *file)
975{
976 kfree(file->private_data);
977
978 return 0;
979}
980
981static const struct file_operations u32_array_fops = {
982 .owner = THIS_MODULE,
983 .open = u32_array_open,
984 .release = u32_array_release,
985 .read = u32_array_read,
986 .llseek = no_llseek,
987};
988
989/**
990 * debugfs_create_u32_array - create a debugfs file that is used to read u32
991 * array.
992 * @name: a pointer to a string containing the name of the file to create.
993 * @mode: the permission that the file should have.
994 * @parent: a pointer to the parent dentry for this file. This should be a
995 * directory dentry if set. If this parameter is %NULL, then the
996 * file will be created in the root of the debugfs filesystem.
997 * @array: u32 array that provides data.
998 * @elements: total number of elements in the array.
999 *
1000 * This function creates a file in debugfs with the given name that exports
1001 * @array as data. If the @mode variable is so set it can be read from.
1002 * Writing is not supported. Seek within the file is also not supported.
1003 * Once array is created its size can not be changed.
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301004 */
Greg Kroah-Hartmanc9c2c272019-04-16 15:46:55 +02001005void debugfs_create_u32_array(const char *name, umode_t mode,
1006 struct dentry *parent, u32 *array, u32 elements)
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301007{
1008 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
1009
1010 if (data == NULL)
Greg Kroah-Hartmanc9c2c272019-04-16 15:46:55 +02001011 return;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301012
1013 data->array = array;
1014 data->elements = elements;
1015
Greg Kroah-Hartmanc9c2c272019-04-16 15:46:55 +02001016 debugfs_create_file_unsafe(name, mode, parent, data, &u32_array_fops);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301017}
1018EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1019
Heiko Carstens3b85e4a2011-12-27 15:08:28 +01001020#ifdef CONFIG_HAS_IOMEM
1021
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001022/*
1023 * The regset32 stuff is used to print 32-bit registers using the
1024 * seq_file utilities. We offer printing a register set in an already-opened
1025 * sequential file or create a debugfs file that only prints a regset32.
1026 */
1027
1028/**
1029 * debugfs_print_regs32 - use seq_print to describe a set of registers
1030 * @s: the seq_file structure being used to generate output
1031 * @regs: an array if struct debugfs_reg32 structures
Randy Dunlapb5763ac2012-01-21 11:02:42 -08001032 * @nregs: the length of the above array
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001033 * @base: the base address to be used in reading the registers
1034 * @prefix: a string to be prefixed to every output line
1035 *
1036 * This function outputs a text block describing the current values of
1037 * some 32-bit hardware registers. It is meant to be used within debugfs
1038 * files based on seq_file that need to show registers, intermixed with other
1039 * information. The prefix argument may be used to specify a leading string,
1040 * because some peripherals have several blocks of identical registers,
1041 * for example configuration of dma channels
1042 */
Joe Perches97615362014-09-29 16:08:26 -07001043void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1044 int nregs, void __iomem *base, char *prefix)
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001045{
Joe Perches97615362014-09-29 16:08:26 -07001046 int i;
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001047
1048 for (i = 0; i < nregs; i++, regs++) {
1049 if (prefix)
Joe Perches97615362014-09-29 16:08:26 -07001050 seq_printf(s, "%s", prefix);
1051 seq_printf(s, "%s = 0x%08x\n", regs->name,
1052 readl(base + regs->offset));
1053 if (seq_has_overflowed(s))
1054 break;
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001055 }
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001056}
1057EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1058
1059static int debugfs_show_regset32(struct seq_file *s, void *data)
1060{
1061 struct debugfs_regset32 *regset = s->private;
1062
1063 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1064 return 0;
1065}
1066
1067static int debugfs_open_regset32(struct inode *inode, struct file *file)
1068{
1069 return single_open(file, debugfs_show_regset32, inode->i_private);
1070}
1071
1072static const struct file_operations fops_regset32 = {
1073 .open = debugfs_open_regset32,
1074 .read = seq_read,
1075 .llseek = seq_lseek,
1076 .release = single_release,
1077};
1078
1079/**
1080 * debugfs_create_regset32 - create a debugfs file that returns register values
1081 * @name: a pointer to a string containing the name of the file to create.
1082 * @mode: the permission that the file should have
1083 * @parent: a pointer to the parent dentry for this file. This should be a
1084 * directory dentry if set. If this parameter is %NULL, then the
1085 * file will be created in the root of the debugfs filesystem.
1086 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1087 * to an array of register definitions, the array size and the base
1088 * address where the register bank is to be found.
1089 *
1090 * This function creates a file in debugfs with the given name that reports
1091 * the names and values of a set of 32-bit registers. If the @mode variable
1092 * is so set it can be read from. Writing is not supported.
1093 *
1094 * This function will return a pointer to a dentry if it succeeds. This
1095 * pointer must be passed to the debugfs_remove() function when the file is
1096 * to be removed (no automatic cleanup happens if your module is unloaded,
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -03001097 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
Ronald Tschalär9abb2492019-04-15 01:25:05 -07001098 * returned.
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001099 *
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -03001100 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
Ronald Tschalär9abb2492019-04-15 01:25:05 -07001101 * be returned.
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001102 */
Al Viro88187392012-03-20 06:00:24 -04001103struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001104 struct dentry *parent,
1105 struct debugfs_regset32 *regset)
1106{
1107 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
1108}
1109EXPORT_SYMBOL_GPL(debugfs_create_regset32);
Heiko Carstens3b85e4a2011-12-27 15:08:28 +01001110
1111#endif /* CONFIG_HAS_IOMEM */
Arend van Spriel98210b72014-11-09 11:31:58 +01001112
1113struct debugfs_devm_entry {
1114 int (*read)(struct seq_file *seq, void *data);
1115 struct device *dev;
1116};
1117
1118static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1119{
1120 struct debugfs_devm_entry *entry = inode->i_private;
1121
1122 return single_open(f, entry->read, entry->dev);
1123}
1124
1125static const struct file_operations debugfs_devm_entry_ops = {
1126 .owner = THIS_MODULE,
1127 .open = debugfs_devm_entry_open,
1128 .release = single_release,
1129 .read = seq_read,
1130 .llseek = seq_lseek
1131};
1132
1133/**
1134 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1135 *
1136 * @dev: device related to this debugfs file.
1137 * @name: name of the debugfs file.
1138 * @parent: a pointer to the parent dentry for this file. This should be a
1139 * directory dentry if set. If this parameter is %NULL, then the
1140 * file will be created in the root of the debugfs filesystem.
1141 * @read_fn: function pointer called to print the seq_file content.
1142 */
1143struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
1144 struct dentry *parent,
1145 int (*read_fn)(struct seq_file *s,
1146 void *data))
1147{
1148 struct debugfs_devm_entry *entry;
1149
1150 if (IS_ERR(parent))
1151 return ERR_PTR(-ENOENT);
1152
1153 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1154 if (!entry)
1155 return ERR_PTR(-ENOMEM);
1156
1157 entry->read = read_fn;
1158 entry->dev = dev;
1159
1160 return debugfs_create_file(name, S_IRUGO, parent, entry,
1161 &debugfs_devm_entry_ops);
1162}
1163EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);