blob: 830a7e76f5c64067e46fad8fd368e9112ddae7a9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * file.c - part of debugfs, a tiny little debug file system
3 *
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 IBM Inc.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * debugfs is for people to use instead of /proc or /sys.
Robert P. J. Day883ce422008-04-25 08:52:51 -040012 * See Documentation/DocBook/filesystems for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 */
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/fs.h>
Alessandro Rubini1a087c62011-11-18 14:50:21 +010018#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/pagemap.h>
Peter Oberparleiter66f54962007-02-13 12:13:54 +010020#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/debugfs.h>
Alessandro Rubini03e099f2011-11-21 10:01:40 +010022#include <linux/io.h>
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +053023#include <linux/slab.h>
Seth Jennings3a76e5e2013-06-03 15:33:02 -050024#include <linux/atomic.h>
Arend van Spriel98210b72014-11-09 11:31:58 +010025#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static ssize_t default_read_file(struct file *file, char __user *buf,
28 size_t count, loff_t *ppos)
29{
30 return 0;
31}
32
33static ssize_t default_write_file(struct file *file, const char __user *buf,
34 size_t count, loff_t *ppos)
35{
36 return count;
37}
38
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080039const struct file_operations debugfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 .read = default_read_file,
41 .write = default_write_file,
Stephen Boyd234e3402012-04-05 14:25:11 -070042 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020043 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
Peter Oberparleiter66f54962007-02-13 12:13:54 +010046static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
47{
David Howells2b0143b2015-03-17 22:25:59 +000048 nd_set_link(nd, d_inode(dentry)->i_private);
Peter Oberparleiter66f54962007-02-13 12:13:54 +010049 return NULL;
50}
51
52const struct inode_operations debugfs_link_operations = {
53 .readlink = generic_readlink,
54 .follow_link = debugfs_follow_link,
55};
56
Christoph Hellwig8b88b092008-02-08 04:20:26 -080057static int debugfs_u8_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +020058{
59 *(u8 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -080060 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +020061}
Christoph Hellwig8b88b092008-02-08 04:20:26 -080062static int debugfs_u8_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +020063{
Christoph Hellwig8b88b092008-02-08 04:20:26 -080064 *val = *(u8 *)data;
65 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +020066}
67DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -040068DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
69DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -070072 * 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 -070073 * @name: a pointer to a string containing the name of the file to create.
74 * @mode: the permission that the file should have
75 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -070076 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * file will be created in the root of the debugfs filesystem.
78 * @value: a pointer to the variable that the file should read to and write
79 * from.
80 *
81 * This function creates a file in debugfs with the given name that
82 * contains the value of the variable @value. If the @mode variable is so
83 * set, it can be read from, and written to.
84 *
85 * This function will return a pointer to a dentry if it succeeds. This
86 * pointer must be passed to the debugfs_remove() function when the file is
87 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -070088 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -070090 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -070092 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * code.
94 */
Al Virof4ae40a2011-07-24 04:33:43 -040095struct dentry *debugfs_create_u8(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct dentry *parent, u8 *value)
97{
Robin Getze4792aa2009-06-02 03:00:47 -040098 /* if there are no write bits set, make read only */
99 if (!(mode & S_IWUGO))
100 return debugfs_create_file(name, mode, parent, value, &fops_u8_ro);
101 /* if there are no read bits set, make write only */
102 if (!(mode & S_IRUGO))
103 return debugfs_create_file(name, mode, parent, value, &fops_u8_wo);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return debugfs_create_file(name, mode, parent, value, &fops_u8);
106}
107EXPORT_SYMBOL_GPL(debugfs_create_u8);
108
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800109static int debugfs_u16_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200110{
111 *(u16 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800112 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200113}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800114static int debugfs_u16_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200115{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800116 *val = *(u16 *)data;
117 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200118}
119DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400120DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
121DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700124 * 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 -0700125 * @name: a pointer to a string containing the name of the file to create.
126 * @mode: the permission that the file should have
127 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700128 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * file will be created in the root of the debugfs filesystem.
130 * @value: a pointer to the variable that the file should read to and write
131 * from.
132 *
133 * This function creates a file in debugfs with the given name that
134 * contains the value of the variable @value. If the @mode variable is so
135 * set, it can be read from, and written to.
136 *
137 * This function will return a pointer to a dentry if it succeeds. This
138 * pointer must be passed to the debugfs_remove() function when the file is
139 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700140 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700142 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700144 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * code.
146 */
Al Virof4ae40a2011-07-24 04:33:43 -0400147struct dentry *debugfs_create_u16(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct dentry *parent, u16 *value)
149{
Robin Getze4792aa2009-06-02 03:00:47 -0400150 /* if there are no write bits set, make read only */
151 if (!(mode & S_IWUGO))
152 return debugfs_create_file(name, mode, parent, value, &fops_u16_ro);
153 /* if there are no read bits set, make write only */
154 if (!(mode & S_IRUGO))
155 return debugfs_create_file(name, mode, parent, value, &fops_u16_wo);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return debugfs_create_file(name, mode, parent, value, &fops_u16);
158}
159EXPORT_SYMBOL_GPL(debugfs_create_u16);
160
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800161static int debugfs_u32_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200162{
163 *(u32 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800164 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200165}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800166static int debugfs_u32_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200167{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800168 *val = *(u32 *)data;
169 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200170}
171DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400172DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
173DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700176 * 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 -0700177 * @name: a pointer to a string containing the name of the file to create.
178 * @mode: the permission that the file should have
179 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700180 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * file will be created in the root of the debugfs filesystem.
182 * @value: a pointer to the variable that the file should read to and write
183 * from.
184 *
185 * This function creates a file in debugfs with the given name that
186 * contains the value of the variable @value. If the @mode variable is so
187 * set, it can be read from, and written to.
188 *
189 * This function will return a pointer to a dentry if it succeeds. This
190 * pointer must be passed to the debugfs_remove() function when the file is
191 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700192 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700194 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700196 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * code.
198 */
Al Virof4ae40a2011-07-24 04:33:43 -0400199struct dentry *debugfs_create_u32(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct dentry *parent, u32 *value)
201{
Robin Getze4792aa2009-06-02 03:00:47 -0400202 /* if there are no write bits set, make read only */
203 if (!(mode & S_IWUGO))
204 return debugfs_create_file(name, mode, parent, value, &fops_u32_ro);
205 /* if there are no read bits set, make write only */
206 if (!(mode & S_IRUGO))
207 return debugfs_create_file(name, mode, parent, value, &fops_u32_wo);
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return debugfs_create_file(name, mode, parent, value, &fops_u32);
210}
211EXPORT_SYMBOL_GPL(debugfs_create_u32);
212
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800213static int debugfs_u64_set(void *data, u64 val)
Michael Ellerman84478912007-04-17 15:59:36 +1000214{
215 *(u64 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800216 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000217}
218
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800219static int debugfs_u64_get(void *data, u64 *val)
Michael Ellerman84478912007-04-17 15:59:36 +1000220{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800221 *val = *(u64 *)data;
222 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000223}
224DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400225DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
226DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
Michael Ellerman84478912007-04-17 15:59:36 +1000227
228/**
229 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
230 * @name: a pointer to a string containing the name of the file to create.
231 * @mode: the permission that the file should have
232 * @parent: a pointer to the parent dentry for this file. This should be a
233 * directory dentry if set. If this parameter is %NULL, then the
234 * file will be created in the root of the debugfs filesystem.
235 * @value: a pointer to the variable that the file should read to and write
236 * from.
237 *
238 * This function creates a file in debugfs with the given name that
239 * contains the value of the variable @value. If the @mode variable is so
240 * set, it can be read from, and written to.
241 *
242 * This function will return a pointer to a dentry if it succeeds. This
243 * pointer must be passed to the debugfs_remove() function when the file is
244 * to be removed (no automatic cleanup happens if your module is unloaded,
245 * you are responsible here.) If an error occurs, %NULL will be returned.
246 *
247 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
248 * returned. It is not wise to check for this value, but rather, check for
249 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
250 * code.
251 */
Al Virof4ae40a2011-07-24 04:33:43 -0400252struct dentry *debugfs_create_u64(const char *name, umode_t mode,
Michael Ellerman84478912007-04-17 15:59:36 +1000253 struct dentry *parent, u64 *value)
254{
Robin Getze4792aa2009-06-02 03:00:47 -0400255 /* if there are no write bits set, make read only */
256 if (!(mode & S_IWUGO))
257 return debugfs_create_file(name, mode, parent, value, &fops_u64_ro);
258 /* if there are no read bits set, make write only */
259 if (!(mode & S_IRUGO))
260 return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);
261
Michael Ellerman84478912007-04-17 15:59:36 +1000262 return debugfs_create_file(name, mode, parent, value, &fops_u64);
263}
264EXPORT_SYMBOL_GPL(debugfs_create_u64);
265
Robin Getz2ebefc52007-08-02 18:23:50 -0400266DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400267DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
268DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400269
270DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400271DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
272DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400273
274DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400275DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
276DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400277
Huang Ying15b0bea2010-05-18 14:35:23 +0800278DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
279
Randy Dunlape6716b82007-10-15 17:30:19 -0700280/*
Huang Ying15b0bea2010-05-18 14:35:23 +0800281 * 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 -0700282 *
283 * These functions are exactly the same as the above functions (but use a hex
284 * output for the decimal challenged). For details look at the above unsigned
285 * decimal functions.
286 */
287
Robin Getz2ebefc52007-08-02 18:23:50 -0400288/**
289 * 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 -0700290 * @name: a pointer to a string containing the name of the file to create.
291 * @mode: the permission that the file should have
292 * @parent: a pointer to the parent dentry for this file. This should be a
293 * directory dentry if set. If this parameter is %NULL, then the
294 * file will be created in the root of the debugfs filesystem.
295 * @value: a pointer to the variable that the file should read to and write
296 * from.
Robin Getz2ebefc52007-08-02 18:23:50 -0400297 */
Al Virof4ae40a2011-07-24 04:33:43 -0400298struct dentry *debugfs_create_x8(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400299 struct dentry *parent, u8 *value)
300{
Robin Getze4792aa2009-06-02 03:00:47 -0400301 /* if there are no write bits set, make read only */
302 if (!(mode & S_IWUGO))
303 return debugfs_create_file(name, mode, parent, value, &fops_x8_ro);
304 /* if there are no read bits set, make write only */
305 if (!(mode & S_IRUGO))
306 return debugfs_create_file(name, mode, parent, value, &fops_x8_wo);
307
Robin Getz2ebefc52007-08-02 18:23:50 -0400308 return debugfs_create_file(name, mode, parent, value, &fops_x8);
309}
310EXPORT_SYMBOL_GPL(debugfs_create_x8);
311
Randy Dunlape6716b82007-10-15 17:30:19 -0700312/**
313 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
314 * @name: a pointer to a string containing the name of the file to create.
315 * @mode: the permission that the file should have
316 * @parent: a pointer to the parent dentry for this file. This should be a
317 * directory dentry if set. If this parameter is %NULL, then the
318 * file will be created in the root of the debugfs filesystem.
319 * @value: a pointer to the variable that the file should read to and write
320 * from.
321 */
Al Virof4ae40a2011-07-24 04:33:43 -0400322struct dentry *debugfs_create_x16(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400323 struct dentry *parent, u16 *value)
324{
Robin Getze4792aa2009-06-02 03:00:47 -0400325 /* if there are no write bits set, make read only */
326 if (!(mode & S_IWUGO))
327 return debugfs_create_file(name, mode, parent, value, &fops_x16_ro);
328 /* if there are no read bits set, make write only */
329 if (!(mode & S_IRUGO))
330 return debugfs_create_file(name, mode, parent, value, &fops_x16_wo);
331
Robin Getz2ebefc52007-08-02 18:23:50 -0400332 return debugfs_create_file(name, mode, parent, value, &fops_x16);
333}
334EXPORT_SYMBOL_GPL(debugfs_create_x16);
335
Randy Dunlape6716b82007-10-15 17:30:19 -0700336/**
337 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
338 * @name: a pointer to a string containing the name of the file to create.
339 * @mode: the permission that the file should have
340 * @parent: a pointer to the parent dentry for this file. This should be a
341 * directory dentry if set. If this parameter is %NULL, then the
342 * file will be created in the root of the debugfs filesystem.
343 * @value: a pointer to the variable that the file should read to and write
344 * from.
345 */
Al Virof4ae40a2011-07-24 04:33:43 -0400346struct dentry *debugfs_create_x32(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400347 struct dentry *parent, u32 *value)
348{
Robin Getze4792aa2009-06-02 03:00:47 -0400349 /* if there are no write bits set, make read only */
350 if (!(mode & S_IWUGO))
351 return debugfs_create_file(name, mode, parent, value, &fops_x32_ro);
352 /* if there are no read bits set, make write only */
353 if (!(mode & S_IRUGO))
354 return debugfs_create_file(name, mode, parent, value, &fops_x32_wo);
355
Robin Getz2ebefc52007-08-02 18:23:50 -0400356 return debugfs_create_file(name, mode, parent, value, &fops_x32);
357}
358EXPORT_SYMBOL_GPL(debugfs_create_x32);
359
Huang Ying15b0bea2010-05-18 14:35:23 +0800360/**
361 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
362 * @name: a pointer to a string containing the name of the file to create.
363 * @mode: the permission that the file should have
364 * @parent: a pointer to the parent dentry for this file. This should be a
365 * directory dentry if set. If this parameter is %NULL, then the
366 * file will be created in the root of the debugfs filesystem.
367 * @value: a pointer to the variable that the file should read to and write
368 * from.
369 */
Al Virof4ae40a2011-07-24 04:33:43 -0400370struct dentry *debugfs_create_x64(const char *name, umode_t mode,
Huang Ying15b0bea2010-05-18 14:35:23 +0800371 struct dentry *parent, u64 *value)
372{
373 return debugfs_create_file(name, mode, parent, value, &fops_x64);
374}
375EXPORT_SYMBOL_GPL(debugfs_create_x64);
376
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800377
378static int debugfs_size_t_set(void *data, u64 val)
379{
380 *(size_t *)data = val;
381 return 0;
382}
383static int debugfs_size_t_get(void *data, u64 *val)
384{
385 *val = *(size_t *)data;
386 return 0;
387}
388DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
389 "%llu\n"); /* %llu and %zu are more or less the same */
390
391/**
392 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
393 * @name: a pointer to a string containing the name of the file to create.
394 * @mode: the permission that the file should have
395 * @parent: a pointer to the parent dentry for this file. This should be a
396 * directory dentry if set. If this parameter is %NULL, then the
397 * file will be created in the root of the debugfs filesystem.
398 * @value: a pointer to the variable that the file should read to and write
399 * from.
400 */
Al Virof4ae40a2011-07-24 04:33:43 -0400401struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800402 struct dentry *parent, size_t *value)
403{
404 return debugfs_create_file(name, mode, parent, value, &fops_size_t);
405}
406EXPORT_SYMBOL_GPL(debugfs_create_size_t);
407
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500408static int debugfs_atomic_t_set(void *data, u64 val)
409{
410 atomic_set((atomic_t *)data, val);
411 return 0;
412}
413static int debugfs_atomic_t_get(void *data, u64 *val)
414{
415 *val = atomic_read((atomic_t *)data);
416 return 0;
417}
418DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
419 debugfs_atomic_t_set, "%lld\n");
420DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
421DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
422
423/**
424 * debugfs_create_atomic_t - create a debugfs file that is used to read and
425 * write an atomic_t value
426 * @name: a pointer to a string containing the name of the file to create.
427 * @mode: the permission that the file should have
428 * @parent: a pointer to the parent dentry for this file. This should be a
429 * directory dentry if set. If this parameter is %NULL, then the
430 * file will be created in the root of the debugfs filesystem.
431 * @value: a pointer to the variable that the file should read to and write
432 * from.
433 */
434struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
435 struct dentry *parent, atomic_t *value)
436{
437 /* if there are no write bits set, make read only */
438 if (!(mode & S_IWUGO))
439 return debugfs_create_file(name, mode, parent, value,
440 &fops_atomic_t_ro);
441 /* if there are no read bits set, make write only */
442 if (!(mode & S_IRUGO))
443 return debugfs_create_file(name, mode, parent, value,
444 &fops_atomic_t_wo);
445
446 return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
447}
448EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static ssize_t read_file_bool(struct file *file, char __user *user_buf,
451 size_t count, loff_t *ppos)
452{
453 char buf[3];
454 u32 *val = file->private_data;
Rahul Bedarkar88e412e2014-06-06 23:12:04 +0530455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (*val)
457 buf[0] = 'Y';
458 else
459 buf[0] = 'N';
460 buf[1] = '\n';
461 buf[2] = 0x00;
462 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
463}
464
465static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
466 size_t count, loff_t *ppos)
467{
468 char buf[32];
Stephen Boydc42d2232011-05-12 16:50:07 -0700469 size_t buf_size;
Jonathan Cameron8705b482011-04-19 12:43:46 +0100470 bool bv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 u32 *val = file->private_data;
472
473 buf_size = min(count, (sizeof(buf)-1));
474 if (copy_from_user(buf, user_buf, buf_size))
475 return -EFAULT;
476
Mathias Krausea3b2c8c72013-05-31 23:24:29 +0200477 buf[buf_size] = '\0';
Jonathan Cameron8705b482011-04-19 12:43:46 +0100478 if (strtobool(buf, &bv) == 0)
479 *val = bv;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return count;
482}
483
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800484static const struct file_operations fops_bool = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 .read = read_file_bool,
486 .write = write_file_bool,
Stephen Boyd234e3402012-04-05 14:25:11 -0700487 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200488 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489};
490
491/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700492 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * @name: a pointer to a string containing the name of the file to create.
494 * @mode: the permission that the file should have
495 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700496 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * file will be created in the root of the debugfs filesystem.
498 * @value: a pointer to the variable that the file should read to and write
499 * from.
500 *
501 * This function creates a file in debugfs with the given name that
502 * contains the value of the variable @value. If the @mode variable is so
503 * set, it can be read from, and written to.
504 *
505 * This function will return a pointer to a dentry if it succeeds. This
506 * pointer must be passed to the debugfs_remove() function when the file is
507 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700508 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700510 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700512 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * code.
514 */
Al Virof4ae40a2011-07-24 04:33:43 -0400515struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct dentry *parent, u32 *value)
517{
518 return debugfs_create_file(name, mode, parent, value, &fops_bool);
519}
520EXPORT_SYMBOL_GPL(debugfs_create_bool);
521
Michael Ellermandd308bc2006-03-07 21:41:59 +1100522static ssize_t read_file_blob(struct file *file, char __user *user_buf,
523 size_t count, loff_t *ppos)
524{
525 struct debugfs_blob_wrapper *blob = file->private_data;
526 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
527 blob->size);
528}
529
Arjan van de Ven00977a52007-02-12 00:55:34 -0800530static const struct file_operations fops_blob = {
Michael Ellermandd308bc2006-03-07 21:41:59 +1100531 .read = read_file_blob,
Stephen Boyd234e3402012-04-05 14:25:11 -0700532 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200533 .llseek = default_llseek,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100534};
535
536/**
Jonathan Corbet400ced62009-05-25 10:15:27 -0600537 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
Michael Ellermandd308bc2006-03-07 21:41:59 +1100538 * @name: a pointer to a string containing the name of the file to create.
539 * @mode: the permission that the file should have
540 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700541 * directory dentry if set. If this parameter is %NULL, then the
Michael Ellermandd308bc2006-03-07 21:41:59 +1100542 * file will be created in the root of the debugfs filesystem.
543 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
544 * to the blob data and the size of the data.
545 *
546 * This function creates a file in debugfs with the given name that exports
547 * @blob->data as a binary blob. If the @mode variable is so set it can be
548 * read from. Writing is not supported.
549 *
550 * This function will return a pointer to a dentry if it succeeds. This
551 * pointer must be passed to the debugfs_remove() function when the file is
552 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700553 * you are responsible here.) If an error occurs, %NULL will be returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +1100554 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700555 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Michael Ellermandd308bc2006-03-07 21:41:59 +1100556 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700557 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Michael Ellermandd308bc2006-03-07 21:41:59 +1100558 * code.
559 */
Al Virof4ae40a2011-07-24 04:33:43 -0400560struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100561 struct dentry *parent,
562 struct debugfs_blob_wrapper *blob)
563{
564 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
565}
566EXPORT_SYMBOL_GPL(debugfs_create_blob);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100567
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530568struct array_data {
569 void *array;
570 u32 elements;
571};
572
Linus Torvaldse05e2792012-09-21 11:48:05 -0700573static size_t u32_format_array(char *buf, size_t bufsize,
574 u32 *array, int array_size)
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530575{
576 size_t ret = 0;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530577
Linus Torvaldse05e2792012-09-21 11:48:05 -0700578 while (--array_size >= 0) {
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530579 size_t len;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700580 char term = array_size ? ' ' : '\n';
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530581
Linus Torvaldse05e2792012-09-21 11:48:05 -0700582 len = snprintf(buf, bufsize, "%u%c", *array++, term);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530583 ret += len;
584
Linus Torvaldse05e2792012-09-21 11:48:05 -0700585 buf += len;
586 bufsize -= len;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530587 }
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530588 return ret;
589}
590
David Rientjes36048852012-09-21 02:16:29 -0700591static int u32_array_open(struct inode *inode, struct file *file)
592{
593 struct array_data *data = inode->i_private;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700594 int size, elements = data->elements;
595 char *buf;
David Rientjes36048852012-09-21 02:16:29 -0700596
Linus Torvaldse05e2792012-09-21 11:48:05 -0700597 /*
598 * Max size:
599 * - 10 digits + ' '/'\n' = 11 bytes per number
600 * - terminating NUL character
601 */
602 size = elements*11;
603 buf = kmalloc(size+1, GFP_KERNEL);
604 if (!buf)
David Rientjes36048852012-09-21 02:16:29 -0700605 return -ENOMEM;
Linus Torvaldse05e2792012-09-21 11:48:05 -0700606 buf[size] = 0;
607
608 file->private_data = buf;
609 u32_format_array(buf, size, data->array, data->elements);
610
David Rientjes36048852012-09-21 02:16:29 -0700611 return nonseekable_open(inode, file);
612}
613
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530614static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
615 loff_t *ppos)
616{
David Rientjes36048852012-09-21 02:16:29 -0700617 size_t size = strlen(file->private_data);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530618
619 return simple_read_from_buffer(buf, len, ppos,
620 file->private_data, size);
621}
622
623static int u32_array_release(struct inode *inode, struct file *file)
624{
625 kfree(file->private_data);
626
627 return 0;
628}
629
630static const struct file_operations u32_array_fops = {
631 .owner = THIS_MODULE,
632 .open = u32_array_open,
633 .release = u32_array_release,
634 .read = u32_array_read,
635 .llseek = no_llseek,
636};
637
638/**
639 * debugfs_create_u32_array - create a debugfs file that is used to read u32
640 * array.
641 * @name: a pointer to a string containing the name of the file to create.
642 * @mode: the permission that the file should have.
643 * @parent: a pointer to the parent dentry for this file. This should be a
644 * directory dentry if set. If this parameter is %NULL, then the
645 * file will be created in the root of the debugfs filesystem.
646 * @array: u32 array that provides data.
647 * @elements: total number of elements in the array.
648 *
649 * This function creates a file in debugfs with the given name that exports
650 * @array as data. If the @mode variable is so set it can be read from.
651 * Writing is not supported. Seek within the file is also not supported.
652 * Once array is created its size can not be changed.
653 *
654 * The function returns a pointer to dentry on success. If debugfs is not
655 * enabled in the kernel, the value -%ENODEV will be returned.
656 */
657struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
658 struct dentry *parent,
659 u32 *array, u32 elements)
660{
661 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
662
663 if (data == NULL)
664 return NULL;
665
666 data->array = array;
667 data->elements = elements;
668
669 return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
670}
671EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
672
Heiko Carstens3b85e4a2011-12-27 15:08:28 +0100673#ifdef CONFIG_HAS_IOMEM
674
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100675/*
676 * The regset32 stuff is used to print 32-bit registers using the
677 * seq_file utilities. We offer printing a register set in an already-opened
678 * sequential file or create a debugfs file that only prints a regset32.
679 */
680
681/**
682 * debugfs_print_regs32 - use seq_print to describe a set of registers
683 * @s: the seq_file structure being used to generate output
684 * @regs: an array if struct debugfs_reg32 structures
Randy Dunlapb5763ac2012-01-21 11:02:42 -0800685 * @nregs: the length of the above array
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100686 * @base: the base address to be used in reading the registers
687 * @prefix: a string to be prefixed to every output line
688 *
689 * This function outputs a text block describing the current values of
690 * some 32-bit hardware registers. It is meant to be used within debugfs
691 * files based on seq_file that need to show registers, intermixed with other
692 * information. The prefix argument may be used to specify a leading string,
693 * because some peripherals have several blocks of identical registers,
694 * for example configuration of dma channels
695 */
Joe Perches97615362014-09-29 16:08:26 -0700696void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
697 int nregs, void __iomem *base, char *prefix)
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100698{
Joe Perches97615362014-09-29 16:08:26 -0700699 int i;
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100700
701 for (i = 0; i < nregs; i++, regs++) {
702 if (prefix)
Joe Perches97615362014-09-29 16:08:26 -0700703 seq_printf(s, "%s", prefix);
704 seq_printf(s, "%s = 0x%08x\n", regs->name,
705 readl(base + regs->offset));
706 if (seq_has_overflowed(s))
707 break;
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100708 }
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100709}
710EXPORT_SYMBOL_GPL(debugfs_print_regs32);
711
712static int debugfs_show_regset32(struct seq_file *s, void *data)
713{
714 struct debugfs_regset32 *regset = s->private;
715
716 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
717 return 0;
718}
719
720static int debugfs_open_regset32(struct inode *inode, struct file *file)
721{
722 return single_open(file, debugfs_show_regset32, inode->i_private);
723}
724
725static const struct file_operations fops_regset32 = {
726 .open = debugfs_open_regset32,
727 .read = seq_read,
728 .llseek = seq_lseek,
729 .release = single_release,
730};
731
732/**
733 * debugfs_create_regset32 - create a debugfs file that returns register values
734 * @name: a pointer to a string containing the name of the file to create.
735 * @mode: the permission that the file should have
736 * @parent: a pointer to the parent dentry for this file. This should be a
737 * directory dentry if set. If this parameter is %NULL, then the
738 * file will be created in the root of the debugfs filesystem.
739 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
740 * to an array of register definitions, the array size and the base
741 * address where the register bank is to be found.
742 *
743 * This function creates a file in debugfs with the given name that reports
744 * the names and values of a set of 32-bit registers. If the @mode variable
745 * is so set it can be read from. Writing is not supported.
746 *
747 * This function will return a pointer to a dentry if it succeeds. This
748 * pointer must be passed to the debugfs_remove() function when the file is
749 * to be removed (no automatic cleanup happens if your module is unloaded,
750 * you are responsible here.) If an error occurs, %NULL will be returned.
751 *
752 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
753 * returned. It is not wise to check for this value, but rather, check for
754 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
755 * code.
756 */
Al Viro88187392012-03-20 06:00:24 -0400757struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100758 struct dentry *parent,
759 struct debugfs_regset32 *regset)
760{
761 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
762}
763EXPORT_SYMBOL_GPL(debugfs_create_regset32);
Heiko Carstens3b85e4a2011-12-27 15:08:28 +0100764
765#endif /* CONFIG_HAS_IOMEM */
Arend van Spriel98210b72014-11-09 11:31:58 +0100766
767struct debugfs_devm_entry {
768 int (*read)(struct seq_file *seq, void *data);
769 struct device *dev;
770};
771
772static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
773{
774 struct debugfs_devm_entry *entry = inode->i_private;
775
776 return single_open(f, entry->read, entry->dev);
777}
778
779static const struct file_operations debugfs_devm_entry_ops = {
780 .owner = THIS_MODULE,
781 .open = debugfs_devm_entry_open,
782 .release = single_release,
783 .read = seq_read,
784 .llseek = seq_lseek
785};
786
787/**
788 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
789 *
790 * @dev: device related to this debugfs file.
791 * @name: name of the debugfs file.
792 * @parent: a pointer to the parent dentry for this file. This should be a
793 * directory dentry if set. If this parameter is %NULL, then the
794 * file will be created in the root of the debugfs filesystem.
795 * @read_fn: function pointer called to print the seq_file content.
796 */
797struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
798 struct dentry *parent,
799 int (*read_fn)(struct seq_file *s,
800 void *data))
801{
802 struct debugfs_devm_entry *entry;
803
804 if (IS_ERR(parent))
805 return ERR_PTR(-ENOENT);
806
807 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
808 if (!entry)
809 return ERR_PTR(-ENOMEM);
810
811 entry->read = read_fn;
812 entry->dev = dev;
813
814 return debugfs_create_file(name, S_IRUGO, parent, entry,
815 &debugfs_devm_entry_ops);
816}
817EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
818