blob: 4d74fc72c195daf72e9d9b8ec9a56e62a51f0227 [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>
18#include <linux/pagemap.h>
Peter Oberparleiter66f54962007-02-13 12:13:54 +010019#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/debugfs.h>
21
22static ssize_t default_read_file(struct file *file, char __user *buf,
23 size_t count, loff_t *ppos)
24{
25 return 0;
26}
27
28static ssize_t default_write_file(struct file *file, const char __user *buf,
29 size_t count, loff_t *ppos)
30{
31 return count;
32}
33
34static int default_open(struct inode *inode, struct file *file)
35{
Theodore Ts'o8e18e292006-09-27 01:50:46 -070036 if (inode->i_private)
37 file->private_data = inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 return 0;
40}
41
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080042const struct file_operations debugfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .read = default_read_file,
44 .write = default_write_file,
45 .open = default_open,
46};
47
Peter Oberparleiter66f54962007-02-13 12:13:54 +010048static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
49{
50 nd_set_link(nd, dentry->d_inode->i_private);
51 return NULL;
52}
53
54const struct inode_operations debugfs_link_operations = {
55 .readlink = generic_readlink,
56 .follow_link = debugfs_follow_link,
57};
58
Christoph Hellwig8b88b092008-02-08 04:20:26 -080059static int debugfs_u8_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +020060{
61 *(u8 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -080062 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +020063}
Christoph Hellwig8b88b092008-02-08 04:20:26 -080064static int debugfs_u8_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +020065{
Christoph Hellwig8b88b092008-02-08 04:20:26 -080066 *val = *(u8 *)data;
67 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +020068}
69DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -040070DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
71DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -070074 * 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 -070075 * @name: a pointer to a string containing the name of the file to create.
76 * @mode: the permission that the file should have
77 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -070078 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * file will be created in the root of the debugfs filesystem.
80 * @value: a pointer to the variable that the file should read to and write
81 * from.
82 *
83 * This function creates a file in debugfs with the given name that
84 * contains the value of the variable @value. If the @mode variable is so
85 * set, it can be read from, and written to.
86 *
87 * This function will return a pointer to a dentry if it succeeds. This
88 * pointer must be passed to the debugfs_remove() function when the file is
89 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -070090 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -070092 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -070094 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * code.
96 */
97struct dentry *debugfs_create_u8(const char *name, mode_t mode,
98 struct dentry *parent, u8 *value)
99{
Robin Getze4792aa2009-06-02 03:00:47 -0400100 /* if there are no write bits set, make read only */
101 if (!(mode & S_IWUGO))
102 return debugfs_create_file(name, mode, parent, value, &fops_u8_ro);
103 /* if there are no read bits set, make write only */
104 if (!(mode & S_IRUGO))
105 return debugfs_create_file(name, mode, parent, value, &fops_u8_wo);
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return debugfs_create_file(name, mode, parent, value, &fops_u8);
108}
109EXPORT_SYMBOL_GPL(debugfs_create_u8);
110
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800111static int debugfs_u16_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200112{
113 *(u16 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800114 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200115}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800116static int debugfs_u16_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200117{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800118 *val = *(u16 *)data;
119 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200120}
121DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400122DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
123DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700126 * 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 -0700127 * @name: a pointer to a string containing the name of the file to create.
128 * @mode: the permission that the file should have
129 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700130 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * file will be created in the root of the debugfs filesystem.
132 * @value: a pointer to the variable that the file should read to and write
133 * from.
134 *
135 * This function creates a file in debugfs with the given name that
136 * contains the value of the variable @value. If the @mode variable is so
137 * set, it can be read from, and written to.
138 *
139 * This function will return a pointer to a dentry if it succeeds. This
140 * pointer must be passed to the debugfs_remove() function when the file is
141 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700142 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700144 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700146 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 * code.
148 */
149struct dentry *debugfs_create_u16(const char *name, mode_t mode,
150 struct dentry *parent, u16 *value)
151{
Robin Getze4792aa2009-06-02 03:00:47 -0400152 /* if there are no write bits set, make read only */
153 if (!(mode & S_IWUGO))
154 return debugfs_create_file(name, mode, parent, value, &fops_u16_ro);
155 /* if there are no read bits set, make write only */
156 if (!(mode & S_IRUGO))
157 return debugfs_create_file(name, mode, parent, value, &fops_u16_wo);
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return debugfs_create_file(name, mode, parent, value, &fops_u16);
160}
161EXPORT_SYMBOL_GPL(debugfs_create_u16);
162
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800163static int debugfs_u32_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200164{
165 *(u32 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800166 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200167}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800168static int debugfs_u32_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200169{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800170 *val = *(u32 *)data;
171 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200172}
173DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400174DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
175DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700178 * 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 -0700179 * @name: a pointer to a string containing the name of the file to create.
180 * @mode: the permission that the file should have
181 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700182 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * file will be created in the root of the debugfs filesystem.
184 * @value: a pointer to the variable that the file should read to and write
185 * from.
186 *
187 * This function creates a file in debugfs with the given name that
188 * contains the value of the variable @value. If the @mode variable is so
189 * set, it can be read from, and written to.
190 *
191 * This function will return a pointer to a dentry if it succeeds. This
192 * pointer must be passed to the debugfs_remove() function when the file is
193 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700194 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700196 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700198 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * code.
200 */
201struct dentry *debugfs_create_u32(const char *name, mode_t mode,
202 struct dentry *parent, u32 *value)
203{
Robin Getze4792aa2009-06-02 03:00:47 -0400204 /* if there are no write bits set, make read only */
205 if (!(mode & S_IWUGO))
206 return debugfs_create_file(name, mode, parent, value, &fops_u32_ro);
207 /* if there are no read bits set, make write only */
208 if (!(mode & S_IRUGO))
209 return debugfs_create_file(name, mode, parent, value, &fops_u32_wo);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return debugfs_create_file(name, mode, parent, value, &fops_u32);
212}
213EXPORT_SYMBOL_GPL(debugfs_create_u32);
214
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800215static int debugfs_u64_set(void *data, u64 val)
Michael Ellerman84478912007-04-17 15:59:36 +1000216{
217 *(u64 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800218 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000219}
220
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800221static int debugfs_u64_get(void *data, u64 *val)
Michael Ellerman84478912007-04-17 15:59:36 +1000222{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800223 *val = *(u64 *)data;
224 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000225}
226DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400227DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
228DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
Michael Ellerman84478912007-04-17 15:59:36 +1000229
230/**
231 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
232 * @name: a pointer to a string containing the name of the file to create.
233 * @mode: the permission that the file should have
234 * @parent: a pointer to the parent dentry for this file. This should be a
235 * directory dentry if set. If this parameter is %NULL, then the
236 * file will be created in the root of the debugfs filesystem.
237 * @value: a pointer to the variable that the file should read to and write
238 * from.
239 *
240 * This function creates a file in debugfs with the given name that
241 * contains the value of the variable @value. If the @mode variable is so
242 * set, it can be read from, and written to.
243 *
244 * This function will return a pointer to a dentry if it succeeds. This
245 * pointer must be passed to the debugfs_remove() function when the file is
246 * to be removed (no automatic cleanup happens if your module is unloaded,
247 * you are responsible here.) If an error occurs, %NULL will be returned.
248 *
249 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
250 * returned. It is not wise to check for this value, but rather, check for
251 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
252 * code.
253 */
254struct dentry *debugfs_create_u64(const char *name, mode_t mode,
255 struct dentry *parent, u64 *value)
256{
Robin Getze4792aa2009-06-02 03:00:47 -0400257 /* if there are no write bits set, make read only */
258 if (!(mode & S_IWUGO))
259 return debugfs_create_file(name, mode, parent, value, &fops_u64_ro);
260 /* if there are no read bits set, make write only */
261 if (!(mode & S_IRUGO))
262 return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);
263
Michael Ellerman84478912007-04-17 15:59:36 +1000264 return debugfs_create_file(name, mode, parent, value, &fops_u64);
265}
266EXPORT_SYMBOL_GPL(debugfs_create_u64);
267
Robin Getz2ebefc52007-08-02 18:23:50 -0400268DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400269DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
270DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400271
272DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400273DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
274DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400275
276DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
Robin Getze4792aa2009-06-02 03:00:47 -0400277DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
278DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400279
Randy Dunlape6716b82007-10-15 17:30:19 -0700280/*
281 * debugfs_create_x{8,16,32} - create a debugfs file that is used to read and write an unsigned {8,16,32}-bit value
282 *
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 */
298struct dentry *debugfs_create_x8(const char *name, mode_t mode,
299 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 */
Robin Getz2ebefc52007-08-02 18:23:50 -0400322struct dentry *debugfs_create_x16(const char *name, mode_t mode,
323 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 */
Robin Getz2ebefc52007-08-02 18:23:50 -0400346struct dentry *debugfs_create_x32(const char *name, mode_t mode,
347 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
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800360
361static int debugfs_size_t_set(void *data, u64 val)
362{
363 *(size_t *)data = val;
364 return 0;
365}
366static int debugfs_size_t_get(void *data, u64 *val)
367{
368 *val = *(size_t *)data;
369 return 0;
370}
371DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
372 "%llu\n"); /* %llu and %zu are more or less the same */
373
374/**
375 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
376 * @name: a pointer to a string containing the name of the file to create.
377 * @mode: the permission that the file should have
378 * @parent: a pointer to the parent dentry for this file. This should be a
379 * directory dentry if set. If this parameter is %NULL, then the
380 * file will be created in the root of the debugfs filesystem.
381 * @value: a pointer to the variable that the file should read to and write
382 * from.
383 */
384struct dentry *debugfs_create_size_t(const char *name, mode_t mode,
385 struct dentry *parent, size_t *value)
386{
387 return debugfs_create_file(name, mode, parent, value, &fops_size_t);
388}
389EXPORT_SYMBOL_GPL(debugfs_create_size_t);
390
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392static ssize_t read_file_bool(struct file *file, char __user *user_buf,
393 size_t count, loff_t *ppos)
394{
395 char buf[3];
396 u32 *val = file->private_data;
397
398 if (*val)
399 buf[0] = 'Y';
400 else
401 buf[0] = 'N';
402 buf[1] = '\n';
403 buf[2] = 0x00;
404 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
405}
406
407static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
408 size_t count, loff_t *ppos)
409{
410 char buf[32];
411 int buf_size;
412 u32 *val = file->private_data;
413
414 buf_size = min(count, (sizeof(buf)-1));
415 if (copy_from_user(buf, user_buf, buf_size))
416 return -EFAULT;
417
418 switch (buf[0]) {
419 case 'y':
420 case 'Y':
421 case '1':
422 *val = 1;
423 break;
424 case 'n':
425 case 'N':
426 case '0':
427 *val = 0;
428 break;
429 }
430
431 return count;
432}
433
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800434static const struct file_operations fops_bool = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 .read = read_file_bool,
436 .write = write_file_bool,
437 .open = default_open,
438};
439
440/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700441 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * @name: a pointer to a string containing the name of the file to create.
443 * @mode: the permission that the file should have
444 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700445 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * file will be created in the root of the debugfs filesystem.
447 * @value: a pointer to the variable that the file should read to and write
448 * from.
449 *
450 * This function creates a file in debugfs with the given name that
451 * contains the value of the variable @value. If the @mode variable is so
452 * set, it can be read from, and written to.
453 *
454 * This function will return a pointer to a dentry if it succeeds. This
455 * pointer must be passed to the debugfs_remove() function when the file is
456 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700457 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700459 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700461 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 * code.
463 */
464struct dentry *debugfs_create_bool(const char *name, mode_t mode,
465 struct dentry *parent, u32 *value)
466{
467 return debugfs_create_file(name, mode, parent, value, &fops_bool);
468}
469EXPORT_SYMBOL_GPL(debugfs_create_bool);
470
Michael Ellermandd308bc2006-03-07 21:41:59 +1100471static ssize_t read_file_blob(struct file *file, char __user *user_buf,
472 size_t count, loff_t *ppos)
473{
474 struct debugfs_blob_wrapper *blob = file->private_data;
475 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
476 blob->size);
477}
478
Arjan van de Ven00977a52007-02-12 00:55:34 -0800479static const struct file_operations fops_blob = {
Michael Ellermandd308bc2006-03-07 21:41:59 +1100480 .read = read_file_blob,
481 .open = default_open,
482};
483
484/**
Jonathan Corbet400ced62009-05-25 10:15:27 -0600485 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
Michael Ellermandd308bc2006-03-07 21:41:59 +1100486 * @name: a pointer to a string containing the name of the file to create.
487 * @mode: the permission that the file should have
488 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700489 * directory dentry if set. If this parameter is %NULL, then the
Michael Ellermandd308bc2006-03-07 21:41:59 +1100490 * file will be created in the root of the debugfs filesystem.
491 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
492 * to the blob data and the size of the data.
493 *
494 * This function creates a file in debugfs with the given name that exports
495 * @blob->data as a binary blob. If the @mode variable is so set it can be
496 * read from. Writing is not supported.
497 *
498 * This function will return a pointer to a dentry if it succeeds. This
499 * pointer must be passed to the debugfs_remove() function when the file is
500 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700501 * you are responsible here.) If an error occurs, %NULL will be returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +1100502 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700503 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Michael Ellermandd308bc2006-03-07 21:41:59 +1100504 * returned. It is not wise to check for this value, but rather, check for
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700505 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
Michael Ellermandd308bc2006-03-07 21:41:59 +1100506 * code.
507 */
508struct dentry *debugfs_create_blob(const char *name, mode_t mode,
509 struct dentry *parent,
510 struct debugfs_blob_wrapper *blob)
511{
512 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
513}
514EXPORT_SYMBOL_GPL(debugfs_create_blob);