blob: 5dfafdd1dbd3cbdd553ef3f88fce4d0ff2fe54a5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static ssize_t default_read_file(struct file *file, char __user *buf,
25 size_t count, loff_t *ppos)
26{
27 return 0;
28}
29
30static ssize_t default_write_file(struct file *file, const char __user *buf,
31 size_t count, loff_t *ppos)
32{
33 return count;
34}
35
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080036const struct file_operations debugfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 .read = default_read_file,
38 .write = default_write_file,
Stephen Boyd234e3402012-04-05 14:25:11 -070039 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020040 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041};
42
Peter Oberparleiter66f54962007-02-13 12:13:54 +010043static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
44{
45 nd_set_link(nd, dentry->d_inode->i_private);
46 return NULL;
47}
48
49const struct inode_operations debugfs_link_operations = {
50 .readlink = generic_readlink,
51 .follow_link = debugfs_follow_link,
52};
53
Christoph Hellwig8b88b092008-02-08 04:20:26 -080054static int debugfs_u8_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +020055{
56 *(u8 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -080057 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +020058}
Christoph Hellwig8b88b092008-02-08 04:20:26 -080059static int debugfs_u8_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +020060{
Christoph Hellwig8b88b092008-02-08 04:20:26 -080061 *val = *(u8 *)data;
62 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +020063}
64DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -040065DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
66DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -070069 * 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 -070070 * @name: a pointer to a string containing the name of the file to create.
71 * @mode: the permission that the file should have
72 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -070073 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * file will be created in the root of the debugfs filesystem.
75 * @value: a pointer to the variable that the file should read to and write
76 * from.
77 *
78 * This function creates a file in debugfs with the given name that
79 * contains the value of the variable @value. If the @mode variable is so
80 * set, it can be read from, and written to.
81 *
82 * This function will return a pointer to a dentry if it succeeds. This
83 * pointer must be passed to the debugfs_remove() function when the file is
84 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -070085 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -070087 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -070089 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * code.
91 */
Al Virof4ae40a62011-07-24 04:33:43 -040092struct dentry *debugfs_create_u8(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct dentry *parent, u8 *value)
94{
Robin Getze4792aa2009-06-02 03:00:47 -040095 /* if there are no write bits set, make read only */
96 if (!(mode & S_IWUGO))
97 return debugfs_create_file(name, mode, parent, value, &fops_u8_ro);
98 /* if there are no read bits set, make write only */
99 if (!(mode & S_IRUGO))
100 return debugfs_create_file(name, mode, parent, value, &fops_u8_wo);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return debugfs_create_file(name, mode, parent, value, &fops_u8);
103}
104EXPORT_SYMBOL_GPL(debugfs_create_u8);
105
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800106static int debugfs_u16_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200107{
108 *(u16 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800109 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200110}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800111static int debugfs_u16_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200112{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800113 *val = *(u16 *)data;
114 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200115}
116DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400117DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
118DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700121 * 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 -0700122 * @name: a pointer to a string containing the name of the file to create.
123 * @mode: the permission that the file should have
124 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700125 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * file will be created in the root of the debugfs filesystem.
127 * @value: a pointer to the variable that the file should read to and write
128 * from.
129 *
130 * This function creates a file in debugfs with the given name that
131 * contains the value of the variable @value. If the @mode variable is so
132 * set, it can be read from, and written to.
133 *
134 * This function will return a pointer to a dentry if it succeeds. This
135 * pointer must be passed to the debugfs_remove() function when the file is
136 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700137 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700139 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700141 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 * code.
143 */
Al Virof4ae40a62011-07-24 04:33:43 -0400144struct dentry *debugfs_create_u16(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct dentry *parent, u16 *value)
146{
Robin Getze4792aa2009-06-02 03:00:47 -0400147 /* if there are no write bits set, make read only */
148 if (!(mode & S_IWUGO))
149 return debugfs_create_file(name, mode, parent, value, &fops_u16_ro);
150 /* if there are no read bits set, make write only */
151 if (!(mode & S_IRUGO))
152 return debugfs_create_file(name, mode, parent, value, &fops_u16_wo);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return debugfs_create_file(name, mode, parent, value, &fops_u16);
155}
156EXPORT_SYMBOL_GPL(debugfs_create_u16);
157
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800158static int debugfs_u32_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200159{
160 *(u32 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800161 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200162}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800163static int debugfs_u32_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200164{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800165 *val = *(u32 *)data;
166 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200167}
168DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400169DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
170DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700173 * 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 -0700174 * @name: a pointer to a string containing the name of the file to create.
175 * @mode: the permission that the file should have
176 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700177 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * file will be created in the root of the debugfs filesystem.
179 * @value: a pointer to the variable that the file should read to and write
180 * from.
181 *
182 * This function creates a file in debugfs with the given name that
183 * contains the value of the variable @value. If the @mode variable is so
184 * set, it can be read from, and written to.
185 *
186 * This function will return a pointer to a dentry if it succeeds. This
187 * pointer must be passed to the debugfs_remove() function when the file is
188 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700189 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700191 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700193 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * code.
195 */
Al Virof4ae40a62011-07-24 04:33:43 -0400196struct dentry *debugfs_create_u32(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct dentry *parent, u32 *value)
198{
Robin Getze4792aa2009-06-02 03:00:47 -0400199 /* if there are no write bits set, make read only */
200 if (!(mode & S_IWUGO))
201 return debugfs_create_file(name, mode, parent, value, &fops_u32_ro);
202 /* if there are no read bits set, make write only */
203 if (!(mode & S_IRUGO))
204 return debugfs_create_file(name, mode, parent, value, &fops_u32_wo);
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return debugfs_create_file(name, mode, parent, value, &fops_u32);
207}
208EXPORT_SYMBOL_GPL(debugfs_create_u32);
209
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800210static int debugfs_u64_set(void *data, u64 val)
Michael Ellerman84478912007-04-17 15:59:36 +1000211{
212 *(u64 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800213 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000214}
215
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800216static int debugfs_u64_get(void *data, u64 *val)
Michael Ellerman84478912007-04-17 15:59:36 +1000217{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800218 *val = *(u64 *)data;
219 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000220}
221DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400222DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
223DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
Michael Ellerman84478912007-04-17 15:59:36 +1000224
225/**
226 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
227 * @name: a pointer to a string containing the name of the file to create.
228 * @mode: the permission that the file should have
229 * @parent: a pointer to the parent dentry for this file. This should be a
230 * directory dentry if set. If this parameter is %NULL, then the
231 * file will be created in the root of the debugfs filesystem.
232 * @value: a pointer to the variable that the file should read to and write
233 * from.
234 *
235 * This function creates a file in debugfs with the given name that
236 * contains the value of the variable @value. If the @mode variable is so
237 * set, it can be read from, and written to.
238 *
239 * This function will return a pointer to a dentry if it succeeds. This
240 * pointer must be passed to the debugfs_remove() function when the file is
241 * to be removed (no automatic cleanup happens if your module is unloaded,
242 * you are responsible here.) If an error occurs, %NULL will be returned.
243 *
244 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
245 * returned. It is not wise to check for this value, but rather, check for
246 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
247 * code.
248 */
Al Virof4ae40a62011-07-24 04:33:43 -0400249struct dentry *debugfs_create_u64(const char *name, umode_t mode,
Michael Ellerman84478912007-04-17 15:59:36 +1000250 struct dentry *parent, u64 *value)
251{
Robin Getze4792aa2009-06-02 03:00:47 -0400252 /* if there are no write bits set, make read only */
253 if (!(mode & S_IWUGO))
254 return debugfs_create_file(name, mode, parent, value, &fops_u64_ro);
255 /* if there are no read bits set, make write only */
256 if (!(mode & S_IRUGO))
257 return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);
258
Michael Ellerman84478912007-04-17 15:59:36 +1000259 return debugfs_create_file(name, mode, parent, value, &fops_u64);
260}
261EXPORT_SYMBOL_GPL(debugfs_create_u64);
262
Robin Getz2ebefc52007-08-02 18:23:50 -0400263DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400264DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
265DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400266
267DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400268DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
269DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400270
271DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400272DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
273DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400274
Huang Ying15b0bea2010-05-18 14:35:23 +0800275DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
276
Randy Dunlape6716b82007-10-15 17:30:19 -0700277/*
Huang Ying15b0bea2010-05-18 14:35:23 +0800278 * 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 -0700279 *
280 * These functions are exactly the same as the above functions (but use a hex
281 * output for the decimal challenged). For details look at the above unsigned
282 * decimal functions.
283 */
284
Robin Getz2ebefc52007-08-02 18:23:50 -0400285/**
286 * 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 -0700287 * @name: a pointer to a string containing the name of the file to create.
288 * @mode: the permission that the file should have
289 * @parent: a pointer to the parent dentry for this file. This should be a
290 * directory dentry if set. If this parameter is %NULL, then the
291 * file will be created in the root of the debugfs filesystem.
292 * @value: a pointer to the variable that the file should read to and write
293 * from.
Robin Getz2ebefc52007-08-02 18:23:50 -0400294 */
Al Virof4ae40a62011-07-24 04:33:43 -0400295struct dentry *debugfs_create_x8(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400296 struct dentry *parent, u8 *value)
297{
Robin Getze4792aa2009-06-02 03:00:47 -0400298 /* if there are no write bits set, make read only */
299 if (!(mode & S_IWUGO))
300 return debugfs_create_file(name, mode, parent, value, &fops_x8_ro);
301 /* if there are no read bits set, make write only */
302 if (!(mode & S_IRUGO))
303 return debugfs_create_file(name, mode, parent, value, &fops_x8_wo);
304
Robin Getz2ebefc52007-08-02 18:23:50 -0400305 return debugfs_create_file(name, mode, parent, value, &fops_x8);
306}
307EXPORT_SYMBOL_GPL(debugfs_create_x8);
308
Randy Dunlape6716b82007-10-15 17:30:19 -0700309/**
310 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
311 * @name: a pointer to a string containing the name of the file to create.
312 * @mode: the permission that the file should have
313 * @parent: a pointer to the parent dentry for this file. This should be a
314 * directory dentry if set. If this parameter is %NULL, then the
315 * file will be created in the root of the debugfs filesystem.
316 * @value: a pointer to the variable that the file should read to and write
317 * from.
318 */
Al Virof4ae40a62011-07-24 04:33:43 -0400319struct dentry *debugfs_create_x16(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400320 struct dentry *parent, u16 *value)
321{
Robin Getze4792aa2009-06-02 03:00:47 -0400322 /* if there are no write bits set, make read only */
323 if (!(mode & S_IWUGO))
324 return debugfs_create_file(name, mode, parent, value, &fops_x16_ro);
325 /* if there are no read bits set, make write only */
326 if (!(mode & S_IRUGO))
327 return debugfs_create_file(name, mode, parent, value, &fops_x16_wo);
328
Robin Getz2ebefc52007-08-02 18:23:50 -0400329 return debugfs_create_file(name, mode, parent, value, &fops_x16);
330}
331EXPORT_SYMBOL_GPL(debugfs_create_x16);
332
Randy Dunlape6716b82007-10-15 17:30:19 -0700333/**
334 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
335 * @name: a pointer to a string containing the name of the file to create.
336 * @mode: the permission that the file should have
337 * @parent: a pointer to the parent dentry for this file. This should be a
338 * directory dentry if set. If this parameter is %NULL, then the
339 * file will be created in the root of the debugfs filesystem.
340 * @value: a pointer to the variable that the file should read to and write
341 * from.
342 */
Al Virof4ae40a62011-07-24 04:33:43 -0400343struct dentry *debugfs_create_x32(const char *name, umode_t mode,
Robin Getz2ebefc52007-08-02 18:23:50 -0400344 struct dentry *parent, u32 *value)
345{
Robin Getze4792aa2009-06-02 03:00:47 -0400346 /* if there are no write bits set, make read only */
347 if (!(mode & S_IWUGO))
348 return debugfs_create_file(name, mode, parent, value, &fops_x32_ro);
349 /* if there are no read bits set, make write only */
350 if (!(mode & S_IRUGO))
351 return debugfs_create_file(name, mode, parent, value, &fops_x32_wo);
352
Robin Getz2ebefc52007-08-02 18:23:50 -0400353 return debugfs_create_file(name, mode, parent, value, &fops_x32);
354}
355EXPORT_SYMBOL_GPL(debugfs_create_x32);
356
Huang Ying15b0bea2010-05-18 14:35:23 +0800357/**
358 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
359 * @name: a pointer to a string containing the name of the file to create.
360 * @mode: the permission that the file should have
361 * @parent: a pointer to the parent dentry for this file. This should be a
362 * directory dentry if set. If this parameter is %NULL, then the
363 * file will be created in the root of the debugfs filesystem.
364 * @value: a pointer to the variable that the file should read to and write
365 * from.
366 */
Al Virof4ae40a62011-07-24 04:33:43 -0400367struct dentry *debugfs_create_x64(const char *name, umode_t mode,
Huang Ying15b0bea2010-05-18 14:35:23 +0800368 struct dentry *parent, u64 *value)
369{
370 return debugfs_create_file(name, mode, parent, value, &fops_x64);
371}
372EXPORT_SYMBOL_GPL(debugfs_create_x64);
373
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800374
375static int debugfs_size_t_set(void *data, u64 val)
376{
377 *(size_t *)data = val;
378 return 0;
379}
380static int debugfs_size_t_get(void *data, u64 *val)
381{
382 *val = *(size_t *)data;
383 return 0;
384}
385DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
386 "%llu\n"); /* %llu and %zu are more or less the same */
387
388/**
389 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
390 * @name: a pointer to a string containing the name of the file to create.
391 * @mode: the permission that the file should have
392 * @parent: a pointer to the parent dentry for this file. This should be a
393 * directory dentry if set. If this parameter is %NULL, then the
394 * file will be created in the root of the debugfs filesystem.
395 * @value: a pointer to the variable that the file should read to and write
396 * from.
397 */
Al Virof4ae40a62011-07-24 04:33:43 -0400398struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800399 struct dentry *parent, size_t *value)
400{
401 return debugfs_create_file(name, mode, parent, value, &fops_size_t);
402}
403EXPORT_SYMBOL_GPL(debugfs_create_size_t);
404
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static ssize_t read_file_bool(struct file *file, char __user *user_buf,
407 size_t count, loff_t *ppos)
408{
409 char buf[3];
410 u32 *val = file->private_data;
411
412 if (*val)
413 buf[0] = 'Y';
414 else
415 buf[0] = 'N';
416 buf[1] = '\n';
417 buf[2] = 0x00;
418 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
419}
420
421static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
422 size_t count, loff_t *ppos)
423{
424 char buf[32];
Stephen Boydc42d2232011-05-12 16:50:07 -0700425 size_t buf_size;
Jonathan Cameron8705b482011-04-19 12:43:46 +0100426 bool bv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 u32 *val = file->private_data;
428
429 buf_size = min(count, (sizeof(buf)-1));
430 if (copy_from_user(buf, user_buf, buf_size))
431 return -EFAULT;
432
Jonathan Cameron8705b482011-04-19 12:43:46 +0100433 if (strtobool(buf, &bv) == 0)
434 *val = bv;
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return count;
437}
438
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800439static const struct file_operations fops_bool = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 .read = read_file_bool,
441 .write = write_file_bool,
Stephen Boyd234e3402012-04-05 14:25:11 -0700442 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200443 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444};
445
446/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700447 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * @name: a pointer to a string containing the name of the file to create.
449 * @mode: the permission that the file should have
450 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700451 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 * file will be created in the root of the debugfs filesystem.
453 * @value: a pointer to the variable that the file should read to and write
454 * from.
455 *
456 * This function creates a file in debugfs with the given name that
457 * contains the value of the variable @value. If the @mode variable is so
458 * set, it can be read from, and written to.
459 *
460 * This function will return a pointer to a dentry if it succeeds. This
461 * pointer must be passed to the debugfs_remove() function when the file is
462 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700463 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700465 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700467 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * code.
469 */
Al Virof4ae40a62011-07-24 04:33:43 -0400470struct dentry *debugfs_create_bool(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct dentry *parent, u32 *value)
472{
473 return debugfs_create_file(name, mode, parent, value, &fops_bool);
474}
475EXPORT_SYMBOL_GPL(debugfs_create_bool);
476
Michael Ellermandd308bc2006-03-07 21:41:59 +1100477static ssize_t read_file_blob(struct file *file, char __user *user_buf,
478 size_t count, loff_t *ppos)
479{
480 struct debugfs_blob_wrapper *blob = file->private_data;
481 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
482 blob->size);
483}
484
Arjan van de Ven00977a52007-02-12 00:55:34 -0800485static const struct file_operations fops_blob = {
Michael Ellermandd308bc2006-03-07 21:41:59 +1100486 .read = read_file_blob,
Stephen Boyd234e3402012-04-05 14:25:11 -0700487 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200488 .llseek = default_llseek,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100489};
490
491/**
Jonathan Corbet400ced62009-05-25 10:15:27 -0600492 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
Michael Ellermandd308bc2006-03-07 21:41:59 +1100493 * @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
Michael Ellermandd308bc2006-03-07 21:41:59 +1100497 * file will be created in the root of the debugfs filesystem.
498 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
499 * to the blob data and the size of the data.
500 *
501 * This function creates a file in debugfs with the given name that exports
502 * @blob->data as a binary blob. If the @mode variable is so set it can be
503 * read from. Writing is not supported.
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.
Michael Ellermandd308bc2006-03-07 21:41:59 +1100509 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700510 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Michael Ellermandd308bc2006-03-07 21:41:59 +1100511 * 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
Michael Ellermandd308bc2006-03-07 21:41:59 +1100513 * code.
514 */
Al Virof4ae40a62011-07-24 04:33:43 -0400515struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +1100516 struct dentry *parent,
517 struct debugfs_blob_wrapper *blob)
518{
519 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
520}
521EXPORT_SYMBOL_GPL(debugfs_create_blob);
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100522
Heiko Carstens3b85e4a2011-12-27 15:08:28 +0100523#ifdef CONFIG_HAS_IOMEM
524
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100525/*
526 * The regset32 stuff is used to print 32-bit registers using the
527 * seq_file utilities. We offer printing a register set in an already-opened
528 * sequential file or create a debugfs file that only prints a regset32.
529 */
530
531/**
532 * debugfs_print_regs32 - use seq_print to describe a set of registers
533 * @s: the seq_file structure being used to generate output
534 * @regs: an array if struct debugfs_reg32 structures
Randy Dunlapb5763ac2012-01-21 11:02:42 -0800535 * @nregs: the length of the above array
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100536 * @base: the base address to be used in reading the registers
537 * @prefix: a string to be prefixed to every output line
538 *
539 * This function outputs a text block describing the current values of
540 * some 32-bit hardware registers. It is meant to be used within debugfs
541 * files based on seq_file that need to show registers, intermixed with other
542 * information. The prefix argument may be used to specify a leading string,
543 * because some peripherals have several blocks of identical registers,
544 * for example configuration of dma channels
545 */
Alessandro Rubini8ee4dd92011-11-18 23:53:29 +0100546int debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100547 int nregs, void __iomem *base, char *prefix)
548{
549 int i, ret = 0;
550
551 for (i = 0; i < nregs; i++, regs++) {
552 if (prefix)
553 ret += seq_printf(s, "%s", prefix);
554 ret += seq_printf(s, "%s = 0x%08x\n", regs->name,
Dan Carpenter58584412011-11-24 10:22:08 +0300555 readl(base + regs->offset));
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100556 }
557 return ret;
558}
559EXPORT_SYMBOL_GPL(debugfs_print_regs32);
560
561static int debugfs_show_regset32(struct seq_file *s, void *data)
562{
563 struct debugfs_regset32 *regset = s->private;
564
565 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
566 return 0;
567}
568
569static int debugfs_open_regset32(struct inode *inode, struct file *file)
570{
571 return single_open(file, debugfs_show_regset32, inode->i_private);
572}
573
574static const struct file_operations fops_regset32 = {
575 .open = debugfs_open_regset32,
576 .read = seq_read,
577 .llseek = seq_lseek,
578 .release = single_release,
579};
580
581/**
582 * debugfs_create_regset32 - create a debugfs file that returns register values
583 * @name: a pointer to a string containing the name of the file to create.
584 * @mode: the permission that the file should have
585 * @parent: a pointer to the parent dentry for this file. This should be a
586 * directory dentry if set. If this parameter is %NULL, then the
587 * file will be created in the root of the debugfs filesystem.
588 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
589 * to an array of register definitions, the array size and the base
590 * address where the register bank is to be found.
591 *
592 * This function creates a file in debugfs with the given name that reports
593 * the names and values of a set of 32-bit registers. If the @mode variable
594 * is so set it can be read from. Writing is not supported.
595 *
596 * This function will return a pointer to a dentry if it succeeds. This
597 * pointer must be passed to the debugfs_remove() function when the file is
598 * to be removed (no automatic cleanup happens if your module is unloaded,
599 * you are responsible here.) If an error occurs, %NULL will be returned.
600 *
601 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
602 * returned. It is not wise to check for this value, but rather, check for
603 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
604 * code.
605 */
Al Viro88187392012-03-20 06:00:24 -0400606struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
Alessandro Rubini1a087c62011-11-18 14:50:21 +0100607 struct dentry *parent,
608 struct debugfs_regset32 *regset)
609{
610 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
611}
612EXPORT_SYMBOL_GPL(debugfs_create_regset32);
Heiko Carstens3b85e4a2011-12-27 15:08:28 +0100613
614#endif /* CONFIG_HAS_IOMEM */