blob: 08511678b78217924688c37ace29d4e405c384b8 [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 Stange9fd4dce2016-03-22 14:11:13 +010025#include <linux/srcu.h>
Nicolai Stange49d200d2016-03-22 14:11:14 +010026#include <asm/poll.h>
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010027
28#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Nicolai Stange49d200d2016-03-22 14:11:14 +010030struct poll_table_struct;
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static ssize_t default_read_file(struct file *file, char __user *buf,
33 size_t count, loff_t *ppos)
34{
35 return 0;
36}
37
38static ssize_t default_write_file(struct file *file, const char __user *buf,
39 size_t count, loff_t *ppos)
40{
41 return count;
42}
43
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010044const struct file_operations debugfs_noop_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .read = default_read_file,
46 .write = default_write_file,
Stephen Boyd234e3402012-04-05 14:25:11 -070047 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020048 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010051/**
52 * debugfs_use_file_start - mark the beginning of file data access
53 * @dentry: the dentry object whose data is being accessed.
54 * @srcu_idx: a pointer to some memory to store a SRCU index in.
55 *
56 * Up to a matching call to debugfs_use_file_finish(), any
57 * successive call into the file removing functions debugfs_remove()
58 * and debugfs_remove_recursive() will block. Since associated private
59 * file data may only get freed after a successful return of any of
60 * the removal functions, you may safely access it after a successful
61 * call to debugfs_use_file_start() without worrying about
62 * lifetime issues.
63 *
64 * If -%EIO is returned, the file has already been removed and thus,
65 * it is not safe to access any of its data. If, on the other hand,
66 * it is allowed to access the file data, zero is returned.
67 *
68 * Regardless of the return code, any call to
69 * debugfs_use_file_start() must be followed by a matching call
70 * to debugfs_use_file_finish().
71 */
Nicolai Stange49d200d2016-03-22 14:11:14 +010072int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010073 __acquires(&debugfs_srcu)
74{
75 *srcu_idx = srcu_read_lock(&debugfs_srcu);
76 barrier();
77 if (d_unlinked(dentry))
78 return -EIO;
79 return 0;
80}
Nicolai Stange49d200d2016-03-22 14:11:14 +010081EXPORT_SYMBOL_GPL(debugfs_use_file_start);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010082
83/**
84 * debugfs_use_file_finish - mark the end of file data access
85 * @srcu_idx: the SRCU index "created" by a former call to
86 * debugfs_use_file_start().
87 *
88 * Allow any ongoing concurrent call into debugfs_remove() or
89 * debugfs_remove_recursive() blocked by a former call to
90 * debugfs_use_file_start() to proceed and return to its caller.
91 */
Nicolai Stange49d200d2016-03-22 14:11:14 +010092void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu)
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010093{
94 srcu_read_unlock(&debugfs_srcu, srcu_idx);
95}
Nicolai Stange49d200d2016-03-22 14:11:14 +010096EXPORT_SYMBOL_GPL(debugfs_use_file_finish);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010097
98#define F_DENTRY(filp) ((filp)->f_path.dentry)
99
Nicolai Stange7c8d4692017-10-31 00:15:47 +0100100const struct file_operations *debugfs_real_fops(const struct file *filp)
Nicolai Stange7c8d4692017-10-31 00:15:47 +0100101{
102 struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
Nicolai Stange055ab8e2017-10-31 00:15:49 +0100103
Nicolai Stange7c8d4692017-10-31 00:15:47 +0100104 return fsd->real_fops;
105}
106EXPORT_SYMBOL_GPL(debugfs_real_fops);
107
Nicolai Stangee9117a52017-10-31 00:15:48 +0100108/**
109 * debugfs_file_get - mark the beginning of file data access
110 * @dentry: the dentry object whose data is being accessed.
111 *
112 * Up to a matching call to debugfs_file_put(), any successive call
113 * into the file removing functions debugfs_remove() and
114 * debugfs_remove_recursive() will block. Since associated private
115 * file data may only get freed after a successful return of any of
116 * the removal functions, you may safely access it after a successful
117 * call to debugfs_file_get() without worrying about lifetime issues.
118 *
119 * If -%EIO is returned, the file has already been removed and thus,
120 * it is not safe to access any of its data. If, on the other hand,
121 * it is allowed to access the file data, zero is returned.
122 */
123int debugfs_file_get(struct dentry *dentry)
124{
125 struct debugfs_fsdata *fsd = dentry->d_fsdata;
126
127 /* Avoid starvation of removers. */
128 if (d_unlinked(dentry))
129 return -EIO;
130
131 if (!refcount_inc_not_zero(&fsd->active_users))
132 return -EIO;
133
134 return 0;
135}
136EXPORT_SYMBOL_GPL(debugfs_file_get);
137
138/**
139 * debugfs_file_put - mark the end of file data access
140 * @dentry: the dentry object formerly passed to
141 * debugfs_file_get().
142 *
143 * Allow any ongoing concurrent call into debugfs_remove() or
144 * debugfs_remove_recursive() blocked by a former call to
145 * debugfs_file_get() to proceed and return to its caller.
146 */
147void debugfs_file_put(struct dentry *dentry)
148{
149 struct debugfs_fsdata *fsd = dentry->d_fsdata;
150
151 if (refcount_dec_and_test(&fsd->active_users))
152 complete(&fsd->active_users_drained);
153}
154EXPORT_SYMBOL_GPL(debugfs_file_put);
155
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100156static int open_proxy_open(struct inode *inode, struct file *filp)
157{
158 const struct dentry *dentry = F_DENTRY(filp);
159 const struct file_operations *real_fops = NULL;
160 int srcu_idx, r;
161
162 r = debugfs_use_file_start(dentry, &srcu_idx);
163 if (r) {
164 r = -ENOENT;
165 goto out;
166 }
167
Christian Lamparter86f0e062016-09-17 21:43:01 +0200168 real_fops = debugfs_real_fops(filp);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100169 real_fops = fops_get(real_fops);
170 if (!real_fops) {
171 /* Huh? Module did not clean up after itself at exit? */
172 WARN(1, "debugfs file owner did not clean up at exit: %pd",
173 dentry);
174 r = -ENXIO;
175 goto out;
176 }
177 replace_fops(filp, real_fops);
178
179 if (real_fops->open)
180 r = real_fops->open(inode, filp);
181
182out:
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100183 debugfs_use_file_finish(srcu_idx);
184 return r;
185}
186
187const struct file_operations debugfs_open_proxy_file_operations = {
188 .open = open_proxy_open,
189};
190
Nicolai Stange49d200d2016-03-22 14:11:14 +0100191#define PROTO(args...) args
192#define ARGS(args...) args
193
194#define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
195static ret_type full_proxy_ ## name(proto) \
196{ \
197 const struct dentry *dentry = F_DENTRY(filp); \
198 const struct file_operations *real_fops = \
Christian Lamparter86f0e062016-09-17 21:43:01 +0200199 debugfs_real_fops(filp); \
Nicolai Stange49d200d2016-03-22 14:11:14 +0100200 int srcu_idx; \
201 ret_type r; \
202 \
203 r = debugfs_use_file_start(dentry, &srcu_idx); \
204 if (likely(!r)) \
205 r = real_fops->name(args); \
206 debugfs_use_file_finish(srcu_idx); \
207 return r; \
208}
209
210FULL_PROXY_FUNC(llseek, loff_t, filp,
211 PROTO(struct file *filp, loff_t offset, int whence),
212 ARGS(filp, offset, whence));
213
214FULL_PROXY_FUNC(read, ssize_t, filp,
215 PROTO(struct file *filp, char __user *buf, size_t size,
216 loff_t *ppos),
217 ARGS(filp, buf, size, ppos));
218
219FULL_PROXY_FUNC(write, ssize_t, filp,
220 PROTO(struct file *filp, const char __user *buf, size_t size,
221 loff_t *ppos),
222 ARGS(filp, buf, size, ppos));
223
224FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
225 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
226 ARGS(filp, cmd, arg));
227
228static unsigned int full_proxy_poll(struct file *filp,
229 struct poll_table_struct *wait)
230{
231 const struct dentry *dentry = F_DENTRY(filp);
Christian Lamparter86f0e062016-09-17 21:43:01 +0200232 const struct file_operations *real_fops = debugfs_real_fops(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100233 int srcu_idx;
234 unsigned int r = 0;
235
236 if (debugfs_use_file_start(dentry, &srcu_idx)) {
237 debugfs_use_file_finish(srcu_idx);
238 return POLLHUP;
239 }
240
241 r = real_fops->poll(filp, wait);
242 debugfs_use_file_finish(srcu_idx);
243 return r;
244}
245
246static int full_proxy_release(struct inode *inode, struct file *filp)
247{
248 const struct dentry *dentry = F_DENTRY(filp);
Christian Lamparter86f0e062016-09-17 21:43:01 +0200249 const struct file_operations *real_fops = debugfs_real_fops(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100250 const struct file_operations *proxy_fops = filp->f_op;
251 int r = 0;
252
253 /*
254 * We must not protect this against removal races here: the
255 * original releaser should be called unconditionally in order
256 * not to leak any resources. Releasers must not assume that
257 * ->i_private is still being meaningful here.
258 */
259 if (real_fops->release)
260 r = real_fops->release(inode, filp);
261
262 replace_fops(filp, d_inode(dentry)->i_fop);
263 kfree((void *)proxy_fops);
264 fops_put(real_fops);
Eric Engestroma1a9e5d2016-09-21 10:27:36 +0100265 return r;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100266}
267
268static void __full_proxy_fops_init(struct file_operations *proxy_fops,
269 const struct file_operations *real_fops)
270{
271 proxy_fops->release = full_proxy_release;
272 if (real_fops->llseek)
273 proxy_fops->llseek = full_proxy_llseek;
274 if (real_fops->read)
275 proxy_fops->read = full_proxy_read;
276 if (real_fops->write)
277 proxy_fops->write = full_proxy_write;
278 if (real_fops->poll)
279 proxy_fops->poll = full_proxy_poll;
280 if (real_fops->unlocked_ioctl)
281 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
282}
283
284static int full_proxy_open(struct inode *inode, struct file *filp)
285{
286 const struct dentry *dentry = F_DENTRY(filp);
287 const struct file_operations *real_fops = NULL;
288 struct file_operations *proxy_fops = NULL;
289 int srcu_idx, r;
290
291 r = debugfs_use_file_start(dentry, &srcu_idx);
292 if (r) {
293 r = -ENOENT;
294 goto out;
295 }
296
Christian Lamparter86f0e062016-09-17 21:43:01 +0200297 real_fops = debugfs_real_fops(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100298 real_fops = fops_get(real_fops);
299 if (!real_fops) {
300 /* Huh? Module did not cleanup after itself at exit? */
301 WARN(1, "debugfs file owner did not clean up at exit: %pd",
302 dentry);
303 r = -ENXIO;
304 goto out;
305 }
306
307 proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
308 if (!proxy_fops) {
309 r = -ENOMEM;
310 goto free_proxy;
311 }
312 __full_proxy_fops_init(proxy_fops, real_fops);
313 replace_fops(filp, proxy_fops);
314
315 if (real_fops->open) {
316 r = real_fops->open(inode, filp);
Nicolai Stangeb10e3e92016-05-24 13:08:53 +0200317 if (r) {
318 replace_fops(filp, d_inode(dentry)->i_fop);
319 goto free_proxy;
320 } else if (filp->f_op != proxy_fops) {
Nicolai Stange49d200d2016-03-22 14:11:14 +0100321 /* No protection against file removal anymore. */
322 WARN(1, "debugfs file owner replaced proxy fops: %pd",
323 dentry);
324 goto free_proxy;
325 }
326 }
327
328 goto out;
329free_proxy:
330 kfree(proxy_fops);
331 fops_put(real_fops);
332out:
333 debugfs_use_file_finish(srcu_idx);
334 return r;
335}
336
337const struct file_operations debugfs_full_proxy_file_operations = {
338 .open = full_proxy_open,
339};
340
Nicolai Stangec6468802016-03-22 14:11:15 +0100341ssize_t debugfs_attr_read(struct file *file, char __user *buf,
342 size_t len, loff_t *ppos)
343{
344 ssize_t ret;
345 int srcu_idx;
346
347 ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
348 if (likely(!ret))
349 ret = simple_attr_read(file, buf, len, ppos);
350 debugfs_use_file_finish(srcu_idx);
351 return ret;
352}
353EXPORT_SYMBOL_GPL(debugfs_attr_read);
354
355ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
356 size_t len, loff_t *ppos)
357{
358 ssize_t ret;
359 int srcu_idx;
360
361 ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
362 if (likely(!ret))
363 ret = simple_attr_write(file, buf, len, ppos);
364 debugfs_use_file_finish(srcu_idx);
365 return ret;
366}
367EXPORT_SYMBOL_GPL(debugfs_attr_write);
368
Nicolai Stange4909f162016-03-22 14:11:17 +0100369static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
370 struct dentry *parent, void *value,
371 const struct file_operations *fops,
372 const struct file_operations *fops_ro,
373 const struct file_operations *fops_wo)
374{
375 /* if there are no write bits set, make read only */
376 if (!(mode & S_IWUGO))
377 return debugfs_create_file_unsafe(name, mode, parent, value,
378 fops_ro);
379 /* if there are no read bits set, make write only */
380 if (!(mode & S_IRUGO))
381 return debugfs_create_file_unsafe(name, mode, parent, value,
382 fops_wo);
383
384 return debugfs_create_file_unsafe(name, mode, parent, value, fops);
385}
386
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800387static int debugfs_u8_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200388{
389 *(u8 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800390 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200391}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800392static int debugfs_u8_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200393{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800394 *val = *(u8 *)data;
395 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200396}
Nicolai Stange4909f162016-03-22 14:11:17 +0100397DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
398DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
399DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700402 * 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 -0700403 * @name: a pointer to a string containing the name of the file to create.
404 * @mode: the permission that the file should have
405 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700406 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * file will be created in the root of the debugfs filesystem.
408 * @value: a pointer to the variable that the file should read to and write
409 * from.
410 *
411 * This function creates a file in debugfs with the given name that
412 * contains the value of the variable @value. If the @mode variable is so
413 * set, it can be read from, and written to.
414 *
415 * This function will return a pointer to a dentry if it succeeds. This
416 * pointer must be passed to the debugfs_remove() function when the file is
417 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700418 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700420 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700422 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * code.
424 */
Al Virof4ae40a62011-07-24 04:33:43 -0400425struct dentry *debugfs_create_u8(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct dentry *parent, u8 *value)
427{
Nicolai Stange4909f162016-03-22 14:11:17 +0100428 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
Stephen Boydb97f6792015-10-12 18:09:09 -0700429 &fops_u8_ro, &fops_u8_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431EXPORT_SYMBOL_GPL(debugfs_create_u8);
432
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800433static int debugfs_u16_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200434{
435 *(u16 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800436 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200437}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800438static int debugfs_u16_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200439{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800440 *val = *(u16 *)data;
441 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200442}
Nicolai Stange4909f162016-03-22 14:11:17 +0100443DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
444DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
445DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700448 * 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 -0700449 * @name: a pointer to a string containing the name of the file to create.
450 * @mode: the permission that the file should have
451 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700452 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 * file will be created in the root of the debugfs filesystem.
454 * @value: a pointer to the variable that the file should read to and write
455 * from.
456 *
457 * This function creates a file in debugfs with the given name that
458 * contains the value of the variable @value. If the @mode variable is so
459 * set, it can be read from, and written to.
460 *
461 * This function will return a pointer to a dentry if it succeeds. This
462 * pointer must be passed to the debugfs_remove() function when the file is
463 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700464 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700466 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700468 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * code.
470 */
Al Virof4ae40a62011-07-24 04:33:43 -0400471struct dentry *debugfs_create_u16(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct dentry *parent, u16 *value)
473{
Nicolai Stange4909f162016-03-22 14:11:17 +0100474 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
Stephen Boydb97f6792015-10-12 18:09:09 -0700475 &fops_u16_ro, &fops_u16_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477EXPORT_SYMBOL_GPL(debugfs_create_u16);
478
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800479static int debugfs_u32_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200480{
481 *(u32 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800482 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200483}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800484static int debugfs_u32_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200485{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800486 *val = *(u32 *)data;
487 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200488}
Nicolai Stange4909f162016-03-22 14:11:17 +0100489DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
490DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
491DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700494 * 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 -0700495 * @name: a pointer to a string containing the name of the file to create.
496 * @mode: the permission that the file should have
497 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700498 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 * file will be created in the root of the debugfs filesystem.
500 * @value: a pointer to the variable that the file should read to and write
501 * from.
502 *
503 * This function creates a file in debugfs with the given name that
504 * contains the value of the variable @value. If the @mode variable is so
505 * set, it can be read from, and written to.
506 *
507 * This function will return a pointer to a dentry if it succeeds. This
508 * pointer must be passed to the debugfs_remove() function when the file is
509 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700510 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700512 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700514 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * code.
516 */
Al Virof4ae40a62011-07-24 04:33:43 -0400517struct dentry *debugfs_create_u32(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 struct dentry *parent, u32 *value)
519{
Nicolai Stange4909f162016-03-22 14:11:17 +0100520 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
Stephen Boydb97f6792015-10-12 18:09:09 -0700521 &fops_u32_ro, &fops_u32_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523EXPORT_SYMBOL_GPL(debugfs_create_u32);
524
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800525static int debugfs_u64_set(void *data, u64 val)
Michael Ellerman84478912007-04-17 15:59:36 +1000526{
527 *(u64 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800528 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000529}
530
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800531static int debugfs_u64_get(void *data, u64 *val)
Michael Ellerman84478912007-04-17 15:59:36 +1000532{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800533 *val = *(u64 *)data;
534 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000535}
Nicolai Stange4909f162016-03-22 14:11:17 +0100536DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
537DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
538DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
Michael Ellerman84478912007-04-17 15:59:36 +1000539
540/**
541 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
542 * @name: a pointer to a string containing the name of the file to create.
543 * @mode: the permission that the file should have
544 * @parent: a pointer to the parent dentry for this file. This should be a
545 * directory dentry if set. If this parameter is %NULL, then the
546 * file will be created in the root of the debugfs filesystem.
547 * @value: a pointer to the variable that the file should read to and write
548 * from.
549 *
550 * This function creates a file in debugfs with the given name that
551 * contains the value of the variable @value. If the @mode variable is so
552 * set, it can be read from, and written to.
553 *
554 * This function will return a pointer to a dentry if it succeeds. This
555 * pointer must be passed to the debugfs_remove() function when the file is
556 * to be removed (no automatic cleanup happens if your module is unloaded,
557 * you are responsible here.) If an error occurs, %NULL will be returned.
558 *
559 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
560 * returned. It is not wise to check for this value, but rather, check for
561 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
562 * code.
563 */
Al Virof4ae40a62011-07-24 04:33:43 -0400564struct dentry *debugfs_create_u64(const char *name, umode_t mode,
Michael Ellerman84478912007-04-17 15:59:36 +1000565 struct dentry *parent, u64 *value)
566{
Nicolai Stange4909f162016-03-22 14:11:17 +0100567 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
Stephen Boydb97f6792015-10-12 18:09:09 -0700568 &fops_u64_ro, &fops_u64_wo);
Michael Ellerman84478912007-04-17 15:59:36 +1000569}
570EXPORT_SYMBOL_GPL(debugfs_create_u64);
571
Viresh Kumarc23fe832015-10-18 22:43:19 +0530572static int debugfs_ulong_set(void *data, u64 val)
573{
574 *(unsigned long *)data = val;
575 return 0;
576}
577
578static int debugfs_ulong_get(void *data, u64 *val)
579{
580 *val = *(unsigned long *)data;
581 return 0;
582}
Nicolai Stange4909f162016-03-22 14:11:17 +0100583DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
584 "%llu\n");
585DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
586DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
Viresh Kumarc23fe832015-10-18 22:43:19 +0530587
588/**
589 * debugfs_create_ulong - create a debugfs file that is used to read and write
590 * an unsigned long value.
591 * @name: a pointer to a string containing the name of the file to create.
592 * @mode: the permission that the file should have
593 * @parent: a pointer to the parent dentry for this file. This should be a
594 * directory dentry if set. If this parameter is %NULL, then the
595 * file will be created in the root of the debugfs filesystem.
596 * @value: a pointer to the variable that the file should read to and write
597 * from.
598 *
599 * This function creates a file in debugfs with the given name that
600 * contains the value of the variable @value. If the @mode variable is so
601 * set, it can be read from, and written to.
602 *
603 * This function will return a pointer to a dentry if it succeeds. This
604 * pointer must be passed to the debugfs_remove() function when the file is
605 * to be removed (no automatic cleanup happens if your module is unloaded,
606 * you are responsible here.) If an error occurs, %NULL will be returned.
607 *
608 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
609 * returned. It is not wise to check for this value, but rather, check for
610 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
611 * code.
612 */
613struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
614 struct dentry *parent, unsigned long *value)
615{
Nicolai Stange4909f162016-03-22 14:11:17 +0100616 return debugfs_create_mode_unsafe(name, mode, parent, value,
617 &fops_ulong, &fops_ulong_ro,
618 &fops_ulong_wo);
Viresh Kumarc23fe832015-10-18 22:43:19 +0530619}
620EXPORT_SYMBOL_GPL(debugfs_create_ulong);
621
Nicolai Stange4909f162016-03-22 14:11:17 +0100622DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
623DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
624DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400625
Nicolai Stange4909f162016-03-22 14:11:17 +0100626DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
627 "0x%04llx\n");
628DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
629DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400630
Nicolai Stange4909f162016-03-22 14:11:17 +0100631DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
632 "0x%08llx\n");
633DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
634DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400635
Nicolai Stange4909f162016-03-22 14:11:17 +0100636DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
637 "0x%016llx\n");
638DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
639DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
Huang Ying15b0bea2010-05-18 14:35:23 +0800640
Randy Dunlape6716b82007-10-15 17:30:19 -0700641/*
Huang Ying15b0bea2010-05-18 14:35:23 +0800642 * 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 -0700643 *
644 * These functions are exactly the same as the above functions (but use a hex
645 * output for the decimal challenged). For details look at the above unsigned
646 * decimal functions.
647 */
648
Robin Getz2ebefc52007-08-02 18:23:50 -0400649/**
650 * 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 -0700651 * @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.
Robin Getz2ebefc52007-08-02 18:23:50 -0400658 */
Al Virof4ae40a62011-07-24 04:33:43 -0400659struct dentry *debugfs_create_x8(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400660 struct dentry *parent, u8 *value)
661{
Nicolai Stange4909f162016-03-22 14:11:17 +0100662 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
Stephen Boydb97f6792015-10-12 18:09:09 -0700663 &fops_x8_ro, &fops_x8_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400664}
665EXPORT_SYMBOL_GPL(debugfs_create_x8);
666
Randy Dunlape6716b82007-10-15 17:30:19 -0700667/**
668 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
669 * @name: a pointer to a string containing the name of the file to create.
670 * @mode: the permission that the file should have
671 * @parent: a pointer to the parent dentry for this file. This should be a
672 * directory dentry if set. If this parameter is %NULL, then the
673 * file will be created in the root of the debugfs filesystem.
674 * @value: a pointer to the variable that the file should read to and write
675 * from.
676 */
Al Virof4ae40a62011-07-24 04:33:43 -0400677struct dentry *debugfs_create_x16(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400678 struct dentry *parent, u16 *value)
679{
Nicolai Stange4909f162016-03-22 14:11:17 +0100680 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
Stephen Boydb97f6792015-10-12 18:09:09 -0700681 &fops_x16_ro, &fops_x16_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400682}
683EXPORT_SYMBOL_GPL(debugfs_create_x16);
684
Randy Dunlape6716b82007-10-15 17:30:19 -0700685/**
686 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
687 * @name: a pointer to a string containing the name of the file to create.
688 * @mode: the permission that the file should have
689 * @parent: a pointer to the parent dentry for this file. This should be a
690 * directory dentry if set. If this parameter is %NULL, then the
691 * file will be created in the root of the debugfs filesystem.
692 * @value: a pointer to the variable that the file should read to and write
693 * from.
694 */
Al Virof4ae40a62011-07-24 04:33:43 -0400695struct dentry *debugfs_create_x32(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400696 struct dentry *parent, u32 *value)
697{
Nicolai Stange4909f162016-03-22 14:11:17 +0100698 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
Stephen Boydb97f6792015-10-12 18:09:09 -0700699 &fops_x32_ro, &fops_x32_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400700}
701EXPORT_SYMBOL_GPL(debugfs_create_x32);
702
Huang Ying15b0bea2010-05-18 14:35:23 +0800703/**
704 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
705 * @name: a pointer to a string containing the name of the file to create.
706 * @mode: the permission that the file should have
707 * @parent: a pointer to the parent dentry for this file. This should be a
708 * directory dentry if set. If this parameter is %NULL, then the
709 * file will be created in the root of the debugfs filesystem.
710 * @value: a pointer to the variable that the file should read to and write
711 * from.
712 */
Al Virof4ae40a62011-07-24 04:33:43 -0400713struct dentry *debugfs_create_x64(const char *name, umode_t mode,
Huang Ying15b0bea2010-05-18 14:35:23 +0800714 struct dentry *parent, u64 *value)
715{
Nicolai Stange4909f162016-03-22 14:11:17 +0100716 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
Stephen Boyd82b7d4f2015-10-12 18:09:10 -0700717 &fops_x64_ro, &fops_x64_wo);
Huang Ying15b0bea2010-05-18 14:35:23 +0800718}
719EXPORT_SYMBOL_GPL(debugfs_create_x64);
720
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800721
722static int debugfs_size_t_set(void *data, u64 val)
723{
724 *(size_t *)data = val;
725 return 0;
726}
727static int debugfs_size_t_get(void *data, u64 *val)
728{
729 *val = *(size_t *)data;
730 return 0;
731}
Nicolai Stange4909f162016-03-22 14:11:17 +0100732DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
733 "%llu\n"); /* %llu and %zu are more or less the same */
734DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
735DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800736
737/**
738 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
739 * @name: a pointer to a string containing the name of the file to create.
740 * @mode: the permission that the file should have
741 * @parent: a pointer to the parent dentry for this file. This should be a
742 * directory dentry if set. If this parameter is %NULL, then the
743 * file will be created in the root of the debugfs filesystem.
744 * @value: a pointer to the variable that the file should read to and write
745 * from.
746 */
Al Virof4ae40a62011-07-24 04:33:43 -0400747struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800748 struct dentry *parent, size_t *value)
749{
Nicolai Stange4909f162016-03-22 14:11:17 +0100750 return debugfs_create_mode_unsafe(name, mode, parent, value,
751 &fops_size_t, &fops_size_t_ro,
752 &fops_size_t_wo);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800753}
754EXPORT_SYMBOL_GPL(debugfs_create_size_t);
755
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500756static int debugfs_atomic_t_set(void *data, u64 val)
757{
758 atomic_set((atomic_t *)data, val);
759 return 0;
760}
761static int debugfs_atomic_t_get(void *data, u64 *val)
762{
763 *val = atomic_read((atomic_t *)data);
764 return 0;
765}
Nicolai Stange4909f162016-03-22 14:11:17 +0100766DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500767 debugfs_atomic_t_set, "%lld\n");
Nicolai Stange4909f162016-03-22 14:11:17 +0100768DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
769 "%lld\n");
770DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
771 "%lld\n");
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500772
773/**
774 * debugfs_create_atomic_t - create a debugfs file that is used to read and
775 * write an atomic_t value
776 * @name: a pointer to a string containing the name of the file to create.
777 * @mode: the permission that the file should have
778 * @parent: a pointer to the parent dentry for this file. This should be a
779 * directory dentry if set. If this parameter is %NULL, then the
780 * file will be created in the root of the debugfs filesystem.
781 * @value: a pointer to the variable that the file should read to and write
782 * from.
783 */
784struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
785 struct dentry *parent, atomic_t *value)
786{
Nicolai Stange4909f162016-03-22 14:11:17 +0100787 return debugfs_create_mode_unsafe(name, mode, parent, value,
788 &fops_atomic_t, &fops_atomic_t_ro,
789 &fops_atomic_t_wo);
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500790}
791EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800792
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100793ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
794 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 char buf[3];
Nicolai Stange4d45f792016-03-22 14:11:18 +0100797 bool val;
798 int r, srcu_idx;
Rahul Bedarkar88e412e2014-06-06 23:12:04 +0530799
Nicolai Stange4d45f792016-03-22 14:11:18 +0100800 r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
801 if (likely(!r))
802 val = *(bool *)file->private_data;
803 debugfs_use_file_finish(srcu_idx);
804 if (r)
805 return r;
806
807 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 buf[0] = 'Y';
809 else
810 buf[0] = 'N';
811 buf[1] = '\n';
812 buf[2] = 0x00;
813 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
814}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100815EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100817ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
818 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 char buf[32];
Stephen Boydc42d2232011-05-12 16:50:07 -0700821 size_t buf_size;
Jonathan Cameron8705b482011-04-19 12:43:46 +0100822 bool bv;
Nicolai Stange4d45f792016-03-22 14:11:18 +0100823 int r, srcu_idx;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700824 bool *val = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 buf_size = min(count, (sizeof(buf)-1));
827 if (copy_from_user(buf, user_buf, buf_size))
828 return -EFAULT;
829
Mathias Krausea3b2c8c72013-05-31 23:24:29 +0200830 buf[buf_size] = '\0';
Nicolai Stange4d45f792016-03-22 14:11:18 +0100831 if (strtobool(buf, &bv) == 0) {
832 r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
833 if (likely(!r))
834 *val = bv;
835 debugfs_use_file_finish(srcu_idx);
836 if (r)
837 return r;
838 }
Jonathan Cameron8705b482011-04-19 12:43:46 +0100839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return count;
841}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100842EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800844static const struct file_operations fops_bool = {
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100845 .read = debugfs_read_file_bool,
846 .write = debugfs_write_file_bool,
Stephen Boyd234e3402012-04-05 14:25:11 -0700847 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200848 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849};
850
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700851static const struct file_operations fops_bool_ro = {
852 .read = debugfs_read_file_bool,
853 .open = simple_open,
854 .llseek = default_llseek,
855};
856
857static const struct file_operations fops_bool_wo = {
858 .write = debugfs_write_file_bool,
859 .open = simple_open,
860 .llseek = default_llseek,
861};
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700864 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 * @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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * file will be created in the root of the debugfs filesystem.
870 * @value: a pointer to the variable that the file should read to and write
871 * from.
872 *
873 * This function creates a file in debugfs with the given name that
874 * contains the value of the variable @value. If the @mode variable is so
875 * set, it can be read from, and written to.
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700882 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * code.
886 */
Al Virof4ae40a62011-07-24 04:33:43 -0400887struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Viresh Kumar621a5f72015-09-26 15:04:07 -0700888 struct dentry *parent, bool *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Nicolai Stange4d45f792016-03-22 14:11:18 +0100890 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700891 &fops_bool_ro, &fops_bool_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893EXPORT_SYMBOL_GPL(debugfs_create_bool);
894
Michael Ellermandd308bc2006-03-07 21:41:59 +1100895static ssize_t read_file_blob(struct file *file, char __user *user_buf,
896 size_t count, loff_t *ppos)
897{
898 struct debugfs_blob_wrapper *blob = file->private_data;
Nicolai Stange83b711cb2016-03-22 14:11:19 +0100899 ssize_t r;
900 int srcu_idx;
901
902 r = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
903 if (likely(!r))
904 r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
905 blob->size);
906 debugfs_use_file_finish(srcu_idx);
907 return r;
Michael Ellermandd308bc2006-03-07 21:41:59 +1100908}
909
Arjan van de Ven00977a52007-02-12 00:55:34 -0800910static const struct file_operations fops_blob = {
Michael Ellermandd308bc2006-03-07 21:41:59 +1100911 .read = read_file_blob,
Stephen Boyd234e3402012-04-05 14:25:11 -0700912 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200913 .llseek = default_llseek,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100914};
915
916/**
Jonathan Corbet400ced62009-05-25 10:15:27 -0600917 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
Michael Ellermandd308bc2006-03-07 21:41:59 +1100918 * @name: a pointer to a string containing the name of the file to create.
919 * @mode: the permission that the file should have
920 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700921 * directory dentry if set. If this parameter is %NULL, then the
Michael Ellermandd308bc2006-03-07 21:41:59 +1100922 * file will be created in the root of the debugfs filesystem.
923 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
924 * to the blob data and the size of the data.
925 *
926 * This function creates a file in debugfs with the given name that exports
927 * @blob->data as a binary blob. If the @mode variable is so set it can be
928 * read from. Writing is not supported.
929 *
930 * This function will return a pointer to a dentry if it succeeds. This
931 * pointer must be passed to the debugfs_remove() function when the file is
932 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700933 * you are responsible here.) If an error occurs, %NULL will be returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +1100934 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700935 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Michael Ellermandd308bc2006-03-07 21:41:59 +1100936 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700937 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Michael Ellermandd308bc2006-03-07 21:41:59 +1100938 * code.
939 */
Al Virof4ae40a62011-07-24 04:33:43 -0400940struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100941 struct dentry *parent,
942 struct debugfs_blob_wrapper *blob)
943{
Nicolai Stange83b711cb2016-03-22 14:11:19 +0100944 return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
Michael Ellermandd308bc2006-03-07 21:41:59 +1100945}
946EXPORT_SYMBOL_GPL(debugfs_create_blob);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100947
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530948struct array_data {
949 void *array;
950 u32 elements;
951};
952
Linus Torvaldse05e2792012-09-21 11:48:05 -0700953static size_t u32_format_array(char *buf, size_t bufsize,
954 u32 *array, int array_size)
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530955{
956 size_t ret = 0;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530957
Linus Torvaldse05e2792012-09-21 11:48:05 -0700958 while (--array_size >= 0) {
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530959 size_t len;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700960 char term = array_size ? ' ' : '\n';
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530961
Linus Torvaldse05e2792012-09-21 11:48:05 -0700962 len = snprintf(buf, bufsize, "%u%c", *array++, term);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530963 ret += len;
964
Linus Torvaldse05e2792012-09-21 11:48:05 -0700965 buf += len;
966 bufsize -= len;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530967 }
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530968 return ret;
969}
970
David Rientjes36048852012-09-21 02:16:29 -0700971static int u32_array_open(struct inode *inode, struct file *file)
972{
973 struct array_data *data = inode->i_private;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700974 int size, elements = data->elements;
975 char *buf;
David Rientjes36048852012-09-21 02:16:29 -0700976
Linus Torvaldse05e2792012-09-21 11:48:05 -0700977 /*
978 * Max size:
979 * - 10 digits + ' '/'\n' = 11 bytes per number
980 * - terminating NUL character
981 */
982 size = elements*11;
983 buf = kmalloc(size+1, GFP_KERNEL);
984 if (!buf)
David Rientjes36048852012-09-21 02:16:29 -0700985 return -ENOMEM;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700986 buf[size] = 0;
987
988 file->private_data = buf;
989 u32_format_array(buf, size, data->array, data->elements);
990
David Rientjes36048852012-09-21 02:16:29 -0700991 return nonseekable_open(inode, file);
992}
993
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530994static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
995 loff_t *ppos)
996{
David Rientjes36048852012-09-21 02:16:29 -0700997 size_t size = strlen(file->private_data);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530998
999 return simple_read_from_buffer(buf, len, ppos,
1000 file->private_data, size);
1001}
1002
1003static int u32_array_release(struct inode *inode, struct file *file)
1004{
1005 kfree(file->private_data);
1006
1007 return 0;
1008}
1009
1010static const struct file_operations u32_array_fops = {
1011 .owner = THIS_MODULE,
1012 .open = u32_array_open,
1013 .release = u32_array_release,
1014 .read = u32_array_read,
1015 .llseek = no_llseek,
1016};
1017
1018/**
1019 * debugfs_create_u32_array - create a debugfs file that is used to read u32
1020 * array.
1021 * @name: a pointer to a string containing the name of the file to create.
1022 * @mode: the permission that the file should have.
1023 * @parent: a pointer to the parent dentry for this file. This should be a
1024 * directory dentry if set. If this parameter is %NULL, then the
1025 * file will be created in the root of the debugfs filesystem.
1026 * @array: u32 array that provides data.
1027 * @elements: total number of elements in the array.
1028 *
1029 * This function creates a file in debugfs with the given name that exports
1030 * @array as data. If the @mode variable is so set it can be read from.
1031 * Writing is not supported. Seek within the file is also not supported.
1032 * Once array is created its size can not be changed.
1033 *
1034 * The function returns a pointer to dentry on success. If debugfs is not
1035 * enabled in the kernel, the value -%ENODEV will be returned.
1036 */
1037struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
1038 struct dentry *parent,
1039 u32 *array, u32 elements)
1040{
1041 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
1042
1043 if (data == NULL)
1044 return NULL;
1045
1046 data->array = array;
1047 data->elements = elements;
1048
Nicolai Stangec4a74f62016-03-22 14:11:20 +01001049 return debugfs_create_file_unsafe(name, mode, parent, data,
1050 &u32_array_fops);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301051}
1052EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1053
Heiko Carstens3b85e4a2011-12-27 15:08:28 +01001054#ifdef CONFIG_HAS_IOMEM
1055
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001056/*
1057 * The regset32 stuff is used to print 32-bit registers using the
1058 * seq_file utilities. We offer printing a register set in an already-opened
1059 * sequential file or create a debugfs file that only prints a regset32.
1060 */
1061
1062/**
1063 * debugfs_print_regs32 - use seq_print to describe a set of registers
1064 * @s: the seq_file structure being used to generate output
1065 * @regs: an array if struct debugfs_reg32 structures
Randy Dunlapb5763ac2012-01-21 11:02:42 -08001066 * @nregs: the length of the above array
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001067 * @base: the base address to be used in reading the registers
1068 * @prefix: a string to be prefixed to every output line
1069 *
1070 * This function outputs a text block describing the current values of
1071 * some 32-bit hardware registers. It is meant to be used within debugfs
1072 * files based on seq_file that need to show registers, intermixed with other
1073 * information. The prefix argument may be used to specify a leading string,
1074 * because some peripherals have several blocks of identical registers,
1075 * for example configuration of dma channels
1076 */
Joe Perches97615362014-09-29 16:08:26 -07001077void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1078 int nregs, void __iomem *base, char *prefix)
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001079{
Joe Perches97615362014-09-29 16:08:26 -07001080 int i;
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001081
1082 for (i = 0; i < nregs; i++, regs++) {
1083 if (prefix)
Joe Perches97615362014-09-29 16:08:26 -07001084 seq_printf(s, "%s", prefix);
1085 seq_printf(s, "%s = 0x%08x\n", regs->name,
1086 readl(base + regs->offset));
1087 if (seq_has_overflowed(s))
1088 break;
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001089 }
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001090}
1091EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1092
1093static int debugfs_show_regset32(struct seq_file *s, void *data)
1094{
1095 struct debugfs_regset32 *regset = s->private;
1096
1097 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1098 return 0;
1099}
1100
1101static int debugfs_open_regset32(struct inode *inode, struct file *file)
1102{
1103 return single_open(file, debugfs_show_regset32, inode->i_private);
1104}
1105
1106static const struct file_operations fops_regset32 = {
1107 .open = debugfs_open_regset32,
1108 .read = seq_read,
1109 .llseek = seq_lseek,
1110 .release = single_release,
1111};
1112
1113/**
1114 * debugfs_create_regset32 - create a debugfs file that returns register values
1115 * @name: a pointer to a string containing the name of the file to create.
1116 * @mode: the permission that the file should have
1117 * @parent: a pointer to the parent dentry for this file. This should be a
1118 * directory dentry if set. If this parameter is %NULL, then the
1119 * file will be created in the root of the debugfs filesystem.
1120 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1121 * to an array of register definitions, the array size and the base
1122 * address where the register bank is to be found.
1123 *
1124 * This function creates a file in debugfs with the given name that reports
1125 * the names and values of a set of 32-bit registers. If the @mode variable
1126 * is so set it can be read from. Writing is not supported.
1127 *
1128 * This function will return a pointer to a dentry if it succeeds. This
1129 * pointer must be passed to the debugfs_remove() function when the file is
1130 * to be removed (no automatic cleanup happens if your module is unloaded,
1131 * you are responsible here.) If an error occurs, %NULL will be returned.
1132 *
1133 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1134 * returned. It is not wise to check for this value, but rather, check for
1135 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1136 * code.
1137 */
Al Viro88187392012-03-20 06:00:24 -04001138struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001139 struct dentry *parent,
1140 struct debugfs_regset32 *regset)
1141{
1142 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
1143}
1144EXPORT_SYMBOL_GPL(debugfs_create_regset32);
Heiko Carstens3b85e4a2011-12-27 15:08:28 +01001145
1146#endif /* CONFIG_HAS_IOMEM */
Arend van Spriel98210b72014-11-09 11:31:58 +01001147
1148struct debugfs_devm_entry {
1149 int (*read)(struct seq_file *seq, void *data);
1150 struct device *dev;
1151};
1152
1153static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1154{
1155 struct debugfs_devm_entry *entry = inode->i_private;
1156
1157 return single_open(f, entry->read, entry->dev);
1158}
1159
1160static const struct file_operations debugfs_devm_entry_ops = {
1161 .owner = THIS_MODULE,
1162 .open = debugfs_devm_entry_open,
1163 .release = single_release,
1164 .read = seq_read,
1165 .llseek = seq_lseek
1166};
1167
1168/**
1169 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1170 *
1171 * @dev: device related to this debugfs file.
1172 * @name: name of the debugfs file.
1173 * @parent: a pointer to the parent dentry for this file. This should be a
1174 * directory dentry if set. If this parameter is %NULL, then the
1175 * file will be created in the root of the debugfs filesystem.
1176 * @read_fn: function pointer called to print the seq_file content.
1177 */
1178struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
1179 struct dentry *parent,
1180 int (*read_fn)(struct seq_file *s,
1181 void *data))
1182{
1183 struct debugfs_devm_entry *entry;
1184
1185 if (IS_ERR(parent))
1186 return ERR_PTR(-ENOENT);
1187
1188 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1189 if (!entry)
1190 return ERR_PTR(-ENOMEM);
1191
1192 entry->read = read_fn;
1193 entry->dev = dev;
1194
1195 return debugfs_create_file(name, S_IRUGO, parent, entry,
1196 &debugfs_devm_entry_ops);
1197}
1198EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1199