debugfs: Pass bool pointer to debugfs_create_bool()
[linux-2.6-block.git] / fs / debugfs / file.c
CommitLineData
1da177e4
LT
1/*
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.
883ce42e 12 * See Documentation/DocBook/filesystems for more details.
1da177e4
LT
13 *
14 */
15
1da177e4
LT
16#include <linux/module.h>
17#include <linux/fs.h>
1a087c6a 18#include <linux/seq_file.h>
1da177e4
LT
19#include <linux/pagemap.h>
20#include <linux/debugfs.h>
03e099fb 21#include <linux/io.h>
9fe2a701 22#include <linux/slab.h>
3a76e5e0 23#include <linux/atomic.h>
98210b7f 24#include <linux/device.h>
1da177e4
LT
25
26static ssize_t default_read_file(struct file *file, char __user *buf,
27 size_t count, loff_t *ppos)
28{
29 return 0;
30}
31
32static ssize_t default_write_file(struct file *file, const char __user *buf,
33 size_t count, loff_t *ppos)
34{
35 return count;
36}
37
4b6f5d20 38const struct file_operations debugfs_file_operations = {
1da177e4
LT
39 .read = default_read_file,
40 .write = default_write_file,
234e3405 41 .open = simple_open,
6038f373 42 .llseek = noop_llseek,
1da177e4
LT
43};
44
8b88b099 45static int debugfs_u8_set(void *data, u64 val)
acaefc25
AB
46{
47 *(u8 *)data = val;
8b88b099 48 return 0;
acaefc25 49}
8b88b099 50static int debugfs_u8_get(void *data, u64 *val)
acaefc25 51{
8b88b099
CH
52 *val = *(u8 *)data;
53 return 0;
acaefc25
AB
54}
55DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
e4792aa3
RG
56DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
57DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
1da177e4
LT
58
59/**
6468b3af 60 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
1da177e4
LT
61 * @name: a pointer to a string containing the name of the file to create.
62 * @mode: the permission that the file should have
63 * @parent: a pointer to the parent dentry for this file. This should be a
6468b3af 64 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
65 * file will be created in the root of the debugfs filesystem.
66 * @value: a pointer to the variable that the file should read to and write
67 * from.
68 *
69 * This function creates a file in debugfs with the given name that
70 * contains the value of the variable @value. If the @mode variable is so
71 * set, it can be read from, and written to.
72 *
73 * This function will return a pointer to a dentry if it succeeds. This
74 * pointer must be passed to the debugfs_remove() function when the file is
75 * to be removed (no automatic cleanup happens if your module is unloaded,
6468b3af 76 * you are responsible here.) If an error occurs, %NULL will be returned.
1da177e4 77 *
6468b3af 78 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1da177e4 79 * returned. It is not wise to check for this value, but rather, check for
6468b3af 80 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1da177e4
LT
81 * code.
82 */
f4ae40a6 83struct dentry *debugfs_create_u8(const char *name, umode_t mode,
1da177e4
LT
84 struct dentry *parent, u8 *value)
85{
e4792aa3
RG
86 /* if there are no write bits set, make read only */
87 if (!(mode & S_IWUGO))
88 return debugfs_create_file(name, mode, parent, value, &fops_u8_ro);
89 /* if there are no read bits set, make write only */
90 if (!(mode & S_IRUGO))
91 return debugfs_create_file(name, mode, parent, value, &fops_u8_wo);
92
1da177e4
LT
93 return debugfs_create_file(name, mode, parent, value, &fops_u8);
94}
95EXPORT_SYMBOL_GPL(debugfs_create_u8);
96
8b88b099 97static int debugfs_u16_set(void *data, u64 val)
acaefc25
AB
98{
99 *(u16 *)data = val;
8b88b099 100 return 0;
acaefc25 101}
8b88b099 102static int debugfs_u16_get(void *data, u64 *val)
acaefc25 103{
8b88b099
CH
104 *val = *(u16 *)data;
105 return 0;
acaefc25
AB
106}
107DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
e4792aa3
RG
108DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
109DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
acaefc25 110
1da177e4 111/**
6468b3af 112 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
1da177e4
LT
113 * @name: a pointer to a string containing the name of the file to create.
114 * @mode: the permission that the file should have
115 * @parent: a pointer to the parent dentry for this file. This should be a
6468b3af 116 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
117 * file will be created in the root of the debugfs filesystem.
118 * @value: a pointer to the variable that the file should read to and write
119 * from.
120 *
121 * This function creates a file in debugfs with the given name that
122 * contains the value of the variable @value. If the @mode variable is so
123 * set, it can be read from, and written to.
124 *
125 * This function will return a pointer to a dentry if it succeeds. This
126 * pointer must be passed to the debugfs_remove() function when the file is
127 * to be removed (no automatic cleanup happens if your module is unloaded,
6468b3af 128 * you are responsible here.) If an error occurs, %NULL will be returned.
1da177e4 129 *
6468b3af 130 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1da177e4 131 * returned. It is not wise to check for this value, but rather, check for
6468b3af 132 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1da177e4
LT
133 * code.
134 */
f4ae40a6 135struct dentry *debugfs_create_u16(const char *name, umode_t mode,
1da177e4
LT
136 struct dentry *parent, u16 *value)
137{
e4792aa3
RG
138 /* if there are no write bits set, make read only */
139 if (!(mode & S_IWUGO))
140 return debugfs_create_file(name, mode, parent, value, &fops_u16_ro);
141 /* if there are no read bits set, make write only */
142 if (!(mode & S_IRUGO))
143 return debugfs_create_file(name, mode, parent, value, &fops_u16_wo);
144
1da177e4
LT
145 return debugfs_create_file(name, mode, parent, value, &fops_u16);
146}
147EXPORT_SYMBOL_GPL(debugfs_create_u16);
148
8b88b099 149static int debugfs_u32_set(void *data, u64 val)
acaefc25
AB
150{
151 *(u32 *)data = val;
8b88b099 152 return 0;
acaefc25 153}
8b88b099 154static int debugfs_u32_get(void *data, u64 *val)
acaefc25 155{
8b88b099
CH
156 *val = *(u32 *)data;
157 return 0;
acaefc25
AB
158}
159DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
e4792aa3
RG
160DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
161DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
acaefc25 162
1da177e4 163/**
6468b3af 164 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
1da177e4
LT
165 * @name: a pointer to a string containing the name of the file to create.
166 * @mode: the permission that the file should have
167 * @parent: a pointer to the parent dentry for this file. This should be a
6468b3af 168 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
169 * file will be created in the root of the debugfs filesystem.
170 * @value: a pointer to the variable that the file should read to and write
171 * from.
172 *
173 * This function creates a file in debugfs with the given name that
174 * contains the value of the variable @value. If the @mode variable is so
175 * set, it can be read from, and written to.
176 *
177 * This function will return a pointer to a dentry if it succeeds. This
178 * pointer must be passed to the debugfs_remove() function when the file is
179 * to be removed (no automatic cleanup happens if your module is unloaded,
6468b3af 180 * you are responsible here.) If an error occurs, %NULL will be returned.
1da177e4 181 *
6468b3af 182 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1da177e4 183 * returned. It is not wise to check for this value, but rather, check for
6468b3af 184 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1da177e4
LT
185 * code.
186 */
f4ae40a6 187struct dentry *debugfs_create_u32(const char *name, umode_t mode,
1da177e4
LT
188 struct dentry *parent, u32 *value)
189{
e4792aa3
RG
190 /* if there are no write bits set, make read only */
191 if (!(mode & S_IWUGO))
192 return debugfs_create_file(name, mode, parent, value, &fops_u32_ro);
193 /* if there are no read bits set, make write only */
194 if (!(mode & S_IRUGO))
195 return debugfs_create_file(name, mode, parent, value, &fops_u32_wo);
196
1da177e4
LT
197 return debugfs_create_file(name, mode, parent, value, &fops_u32);
198}
199EXPORT_SYMBOL_GPL(debugfs_create_u32);
200
8b88b099 201static int debugfs_u64_set(void *data, u64 val)
8447891f
ME
202{
203 *(u64 *)data = val;
8b88b099 204 return 0;
8447891f
ME
205}
206
8b88b099 207static int debugfs_u64_get(void *data, u64 *val)
8447891f 208{
8b88b099
CH
209 *val = *(u64 *)data;
210 return 0;
8447891f
ME
211}
212DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
e4792aa3
RG
213DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
214DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
8447891f
ME
215
216/**
217 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
218 * @name: a pointer to a string containing the name of the file to create.
219 * @mode: the permission that the file should have
220 * @parent: a pointer to the parent dentry for this file. This should be a
221 * directory dentry if set. If this parameter is %NULL, then the
222 * file will be created in the root of the debugfs filesystem.
223 * @value: a pointer to the variable that the file should read to and write
224 * from.
225 *
226 * This function creates a file in debugfs with the given name that
227 * contains the value of the variable @value. If the @mode variable is so
228 * set, it can be read from, and written to.
229 *
230 * This function will return a pointer to a dentry if it succeeds. This
231 * pointer must be passed to the debugfs_remove() function when the file is
232 * to be removed (no automatic cleanup happens if your module is unloaded,
233 * you are responsible here.) If an error occurs, %NULL will be returned.
234 *
235 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
236 * returned. It is not wise to check for this value, but rather, check for
237 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
238 * code.
239 */
f4ae40a6 240struct dentry *debugfs_create_u64(const char *name, umode_t mode,
8447891f
ME
241 struct dentry *parent, u64 *value)
242{
e4792aa3
RG
243 /* if there are no write bits set, make read only */
244 if (!(mode & S_IWUGO))
245 return debugfs_create_file(name, mode, parent, value, &fops_u64_ro);
246 /* if there are no read bits set, make write only */
247 if (!(mode & S_IRUGO))
248 return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);
249
8447891f
ME
250 return debugfs_create_file(name, mode, parent, value, &fops_u64);
251}
252EXPORT_SYMBOL_GPL(debugfs_create_u64);
253
2ebefc50 254DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
e4792aa3
RG
255DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
256DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
2ebefc50
RG
257
258DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
e4792aa3
RG
259DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
260DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
2ebefc50
RG
261
262DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
e4792aa3
RG
263DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
264DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
2ebefc50 265
15b0beaa
HY
266DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
267
e6716b87 268/*
15b0beaa 269 * 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
2ebefc50 270 *
e6716b87
RD
271 * These functions are exactly the same as the above functions (but use a hex
272 * output for the decimal challenged). For details look at the above unsigned
2ebefc50
RG
273 * decimal functions.
274 */
e6716b87
RD
275
276/**
277 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
278 * @name: a pointer to a string containing the name of the file to create.
279 * @mode: the permission that the file should have
280 * @parent: a pointer to the parent dentry for this file. This should be a
281 * directory dentry if set. If this parameter is %NULL, then the
282 * file will be created in the root of the debugfs filesystem.
283 * @value: a pointer to the variable that the file should read to and write
284 * from.
285 */
f4ae40a6 286struct dentry *debugfs_create_x8(const char *name, umode_t mode,
2ebefc50
RG
287 struct dentry *parent, u8 *value)
288{
e4792aa3
RG
289 /* if there are no write bits set, make read only */
290 if (!(mode & S_IWUGO))
291 return debugfs_create_file(name, mode, parent, value, &fops_x8_ro);
292 /* if there are no read bits set, make write only */
293 if (!(mode & S_IRUGO))
294 return debugfs_create_file(name, mode, parent, value, &fops_x8_wo);
295
2ebefc50
RG
296 return debugfs_create_file(name, mode, parent, value, &fops_x8);
297}
298EXPORT_SYMBOL_GPL(debugfs_create_x8);
299
e6716b87
RD
300/**
301 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
302 * @name: a pointer to a string containing the name of the file to create.
303 * @mode: the permission that the file should have
304 * @parent: a pointer to the parent dentry for this file. This should be a
305 * directory dentry if set. If this parameter is %NULL, then the
306 * file will be created in the root of the debugfs filesystem.
307 * @value: a pointer to the variable that the file should read to and write
308 * from.
309 */
f4ae40a6 310struct dentry *debugfs_create_x16(const char *name, umode_t mode,
2ebefc50
RG
311 struct dentry *parent, u16 *value)
312{
e4792aa3
RG
313 /* if there are no write bits set, make read only */
314 if (!(mode & S_IWUGO))
315 return debugfs_create_file(name, mode, parent, value, &fops_x16_ro);
316 /* if there are no read bits set, make write only */
317 if (!(mode & S_IRUGO))
318 return debugfs_create_file(name, mode, parent, value, &fops_x16_wo);
319
2ebefc50
RG
320 return debugfs_create_file(name, mode, parent, value, &fops_x16);
321}
322EXPORT_SYMBOL_GPL(debugfs_create_x16);
323
e6716b87
RD
324/**
325 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
326 * @name: a pointer to a string containing the name of the file to create.
327 * @mode: the permission that the file should have
328 * @parent: a pointer to the parent dentry for this file. This should be a
329 * directory dentry if set. If this parameter is %NULL, then the
330 * file will be created in the root of the debugfs filesystem.
331 * @value: a pointer to the variable that the file should read to and write
332 * from.
333 */
f4ae40a6 334struct dentry *debugfs_create_x32(const char *name, umode_t mode,
2ebefc50
RG
335 struct dentry *parent, u32 *value)
336{
e4792aa3
RG
337 /* if there are no write bits set, make read only */
338 if (!(mode & S_IWUGO))
339 return debugfs_create_file(name, mode, parent, value, &fops_x32_ro);
340 /* if there are no read bits set, make write only */
341 if (!(mode & S_IRUGO))
342 return debugfs_create_file(name, mode, parent, value, &fops_x32_wo);
343
2ebefc50
RG
344 return debugfs_create_file(name, mode, parent, value, &fops_x32);
345}
346EXPORT_SYMBOL_GPL(debugfs_create_x32);
347
15b0beaa
HY
348/**
349 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
350 * @name: a pointer to a string containing the name of the file to create.
351 * @mode: the permission that the file should have
352 * @parent: a pointer to the parent dentry for this file. This should be a
353 * directory dentry if set. If this parameter is %NULL, then the
354 * file will be created in the root of the debugfs filesystem.
355 * @value: a pointer to the variable that the file should read to and write
356 * from.
357 */
f4ae40a6 358struct dentry *debugfs_create_x64(const char *name, umode_t mode,
15b0beaa
HY
359 struct dentry *parent, u64 *value)
360{
361 return debugfs_create_file(name, mode, parent, value, &fops_x64);
362}
363EXPORT_SYMBOL_GPL(debugfs_create_x64);
364
5e078787
IPG
365
366static int debugfs_size_t_set(void *data, u64 val)
367{
368 *(size_t *)data = val;
369 return 0;
370}
371static int debugfs_size_t_get(void *data, u64 *val)
372{
373 *val = *(size_t *)data;
374 return 0;
375}
376DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
377 "%llu\n"); /* %llu and %zu are more or less the same */
378
379/**
380 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
381 * @name: a pointer to a string containing the name of the file to create.
382 * @mode: the permission that the file should have
383 * @parent: a pointer to the parent dentry for this file. This should be a
384 * directory dentry if set. If this parameter is %NULL, then the
385 * file will be created in the root of the debugfs filesystem.
386 * @value: a pointer to the variable that the file should read to and write
387 * from.
388 */
f4ae40a6 389struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
5e078787
IPG
390 struct dentry *parent, size_t *value)
391{
392 return debugfs_create_file(name, mode, parent, value, &fops_size_t);
393}
394EXPORT_SYMBOL_GPL(debugfs_create_size_t);
395
3a76e5e0
SJ
396static int debugfs_atomic_t_set(void *data, u64 val)
397{
398 atomic_set((atomic_t *)data, val);
399 return 0;
400}
401static int debugfs_atomic_t_get(void *data, u64 *val)
402{
403 *val = atomic_read((atomic_t *)data);
404 return 0;
405}
406DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
407 debugfs_atomic_t_set, "%lld\n");
408DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
409DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
410
411/**
412 * debugfs_create_atomic_t - create a debugfs file that is used to read and
413 * write an atomic_t value
414 * @name: a pointer to a string containing the name of the file to create.
415 * @mode: the permission that the file should have
416 * @parent: a pointer to the parent dentry for this file. This should be a
417 * directory dentry if set. If this parameter is %NULL, then the
418 * file will be created in the root of the debugfs filesystem.
419 * @value: a pointer to the variable that the file should read to and write
420 * from.
421 */
422struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
423 struct dentry *parent, atomic_t *value)
424{
425 /* if there are no write bits set, make read only */
426 if (!(mode & S_IWUGO))
427 return debugfs_create_file(name, mode, parent, value,
428 &fops_atomic_t_ro);
429 /* if there are no read bits set, make write only */
430 if (!(mode & S_IRUGO))
431 return debugfs_create_file(name, mode, parent, value,
432 &fops_atomic_t_wo);
433
434 return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
435}
436EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
5e078787 437
0642ef6f
RF
438ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
439 size_t count, loff_t *ppos)
1da177e4
LT
440{
441 char buf[3];
621a5f7a 442 bool *val = file->private_data;
88e412ea 443
1da177e4
LT
444 if (*val)
445 buf[0] = 'Y';
446 else
447 buf[0] = 'N';
448 buf[1] = '\n';
449 buf[2] = 0x00;
450 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
451}
0642ef6f 452EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
1da177e4 453
0642ef6f
RF
454ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
455 size_t count, loff_t *ppos)
1da177e4
LT
456{
457 char buf[32];
c42d2237 458 size_t buf_size;
8705b48e 459 bool bv;
621a5f7a 460 bool *val = file->private_data;
1da177e4
LT
461
462 buf_size = min(count, (sizeof(buf)-1));
463 if (copy_from_user(buf, user_buf, buf_size))
464 return -EFAULT;
465
a3b2c8c7 466 buf[buf_size] = '\0';
8705b48e
JC
467 if (strtobool(buf, &bv) == 0)
468 *val = bv;
469
1da177e4
LT
470 return count;
471}
0642ef6f 472EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
1da177e4 473
4b6f5d20 474static const struct file_operations fops_bool = {
0642ef6f
RF
475 .read = debugfs_read_file_bool,
476 .write = debugfs_write_file_bool,
234e3405 477 .open = simple_open,
6038f373 478 .llseek = default_llseek,
1da177e4
LT
479};
480
481/**
6468b3af 482 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
1da177e4
LT
483 * @name: a pointer to a string containing the name of the file to create.
484 * @mode: the permission that the file should have
485 * @parent: a pointer to the parent dentry for this file. This should be a
6468b3af 486 * directory dentry if set. If this parameter is %NULL, then the
1da177e4
LT
487 * file will be created in the root of the debugfs filesystem.
488 * @value: a pointer to the variable that the file should read to and write
489 * from.
490 *
491 * This function creates a file in debugfs with the given name that
492 * contains the value of the variable @value. If the @mode variable is so
493 * set, it can be read from, and written to.
494 *
495 * This function will return a pointer to a dentry if it succeeds. This
496 * pointer must be passed to the debugfs_remove() function when the file is
497 * to be removed (no automatic cleanup happens if your module is unloaded,
6468b3af 498 * you are responsible here.) If an error occurs, %NULL will be returned.
1da177e4 499 *
6468b3af 500 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
1da177e4 501 * returned. It is not wise to check for this value, but rather, check for
6468b3af 502 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
1da177e4
LT
503 * code.
504 */
f4ae40a6 505struct dentry *debugfs_create_bool(const char *name, umode_t mode,
621a5f7a 506 struct dentry *parent, bool *value)
1da177e4
LT
507{
508 return debugfs_create_file(name, mode, parent, value, &fops_bool);
509}
510EXPORT_SYMBOL_GPL(debugfs_create_bool);
511
dd308bc3
ME
512static ssize_t read_file_blob(struct file *file, char __user *user_buf,
513 size_t count, loff_t *ppos)
514{
515 struct debugfs_blob_wrapper *blob = file->private_data;
516 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
517 blob->size);
518}
519
00977a59 520static const struct file_operations fops_blob = {
dd308bc3 521 .read = read_file_blob,
234e3405 522 .open = simple_open,
6038f373 523 .llseek = default_llseek,
dd308bc3
ME
524};
525
526/**
400ced61 527 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
dd308bc3
ME
528 * @name: a pointer to a string containing the name of the file to create.
529 * @mode: the permission that the file should have
530 * @parent: a pointer to the parent dentry for this file. This should be a
6468b3af 531 * directory dentry if set. If this parameter is %NULL, then the
dd308bc3
ME
532 * file will be created in the root of the debugfs filesystem.
533 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
534 * to the blob data and the size of the data.
535 *
536 * This function creates a file in debugfs with the given name that exports
537 * @blob->data as a binary blob. If the @mode variable is so set it can be
538 * read from. Writing is not supported.
539 *
540 * This function will return a pointer to a dentry if it succeeds. This
541 * pointer must be passed to the debugfs_remove() function when the file is
542 * to be removed (no automatic cleanup happens if your module is unloaded,
6468b3af 543 * you are responsible here.) If an error occurs, %NULL will be returned.
dd308bc3 544 *
6468b3af 545 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
dd308bc3 546 * returned. It is not wise to check for this value, but rather, check for
6468b3af 547 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
dd308bc3
ME
548 * code.
549 */
f4ae40a6 550struct dentry *debugfs_create_blob(const char *name, umode_t mode,
dd308bc3
ME
551 struct dentry *parent,
552 struct debugfs_blob_wrapper *blob)
553{
554 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
555}
556EXPORT_SYMBOL_GPL(debugfs_create_blob);
1a087c6a 557
9fe2a701
SV
558struct array_data {
559 void *array;
560 u32 elements;
561};
562
e05e279e
LT
563static size_t u32_format_array(char *buf, size_t bufsize,
564 u32 *array, int array_size)
9fe2a701
SV
565{
566 size_t ret = 0;
9fe2a701 567
e05e279e 568 while (--array_size >= 0) {
9fe2a701 569 size_t len;
e05e279e 570 char term = array_size ? ' ' : '\n';
9fe2a701 571
e05e279e 572 len = snprintf(buf, bufsize, "%u%c", *array++, term);
9fe2a701
SV
573 ret += len;
574
e05e279e
LT
575 buf += len;
576 bufsize -= len;
9fe2a701 577 }
9fe2a701
SV
578 return ret;
579}
580
36048853 581static int u32_array_open(struct inode *inode, struct file *file)
9fe2a701 582{
9fe2a701 583 struct array_data *data = inode->i_private;
e05e279e
LT
584 int size, elements = data->elements;
585 char *buf;
586
587 /*
588 * Max size:
589 * - 10 digits + ' '/'\n' = 11 bytes per number
590 * - terminating NUL character
591 */
592 size = elements*11;
593 buf = kmalloc(size+1, GFP_KERNEL);
594 if (!buf)
36048853 595 return -ENOMEM;
e05e279e
LT
596 buf[size] = 0;
597
598 file->private_data = buf;
599 u32_format_array(buf, size, data->array, data->elements);
600
36048853
DR
601 return nonseekable_open(inode, file);
602}
9fe2a701 603
36048853
DR
604static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
605 loff_t *ppos)
606{
607 size_t size = strlen(file->private_data);
9fe2a701
SV
608
609 return simple_read_from_buffer(buf, len, ppos,
610 file->private_data, size);
611}
612
613static int u32_array_release(struct inode *inode, struct file *file)
614{
615 kfree(file->private_data);
616
617 return 0;
618}
619
620static const struct file_operations u32_array_fops = {
621 .owner = THIS_MODULE,
622 .open = u32_array_open,
623 .release = u32_array_release,
624 .read = u32_array_read,
625 .llseek = no_llseek,
626};
627
628/**
629 * debugfs_create_u32_array - create a debugfs file that is used to read u32
630 * array.
631 * @name: a pointer to a string containing the name of the file to create.
632 * @mode: the permission that the file should have.
633 * @parent: a pointer to the parent dentry for this file. This should be a
634 * directory dentry if set. If this parameter is %NULL, then the
635 * file will be created in the root of the debugfs filesystem.
636 * @array: u32 array that provides data.
637 * @elements: total number of elements in the array.
638 *
639 * This function creates a file in debugfs with the given name that exports
640 * @array as data. If the @mode variable is so set it can be read from.
641 * Writing is not supported. Seek within the file is also not supported.
642 * Once array is created its size can not be changed.
643 *
644 * The function returns a pointer to dentry on success. If debugfs is not
645 * enabled in the kernel, the value -%ENODEV will be returned.
646 */
647struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
648 struct dentry *parent,
649 u32 *array, u32 elements)
650{
651 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
652
653 if (data == NULL)
654 return NULL;
655
656 data->array = array;
657 data->elements = elements;
658
659 return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
660}
661EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
662
3b85e4ab
HC
663#ifdef CONFIG_HAS_IOMEM
664
1a087c6a
AR
665/*
666 * The regset32 stuff is used to print 32-bit registers using the
667 * seq_file utilities. We offer printing a register set in an already-opened
668 * sequential file or create a debugfs file that only prints a regset32.
669 */
670
671/**
672 * debugfs_print_regs32 - use seq_print to describe a set of registers
673 * @s: the seq_file structure being used to generate output
674 * @regs: an array if struct debugfs_reg32 structures
b5763acc 675 * @nregs: the length of the above array
1a087c6a
AR
676 * @base: the base address to be used in reading the registers
677 * @prefix: a string to be prefixed to every output line
678 *
679 * This function outputs a text block describing the current values of
680 * some 32-bit hardware registers. It is meant to be used within debugfs
681 * files based on seq_file that need to show registers, intermixed with other
682 * information. The prefix argument may be used to specify a leading string,
683 * because some peripherals have several blocks of identical registers,
684 * for example configuration of dma channels
685 */
9761536e
JP
686void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
687 int nregs, void __iomem *base, char *prefix)
1a087c6a 688{
9761536e 689 int i;
1a087c6a
AR
690
691 for (i = 0; i < nregs; i++, regs++) {
692 if (prefix)
9761536e
JP
693 seq_printf(s, "%s", prefix);
694 seq_printf(s, "%s = 0x%08x\n", regs->name,
695 readl(base + regs->offset));
696 if (seq_has_overflowed(s))
697 break;
1a087c6a 698 }
1a087c6a
AR
699}
700EXPORT_SYMBOL_GPL(debugfs_print_regs32);
701
702static int debugfs_show_regset32(struct seq_file *s, void *data)
703{
704 struct debugfs_regset32 *regset = s->private;
705
706 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
707 return 0;
708}
709
710static int debugfs_open_regset32(struct inode *inode, struct file *file)
711{
712 return single_open(file, debugfs_show_regset32, inode->i_private);
713}
714
715static const struct file_operations fops_regset32 = {
716 .open = debugfs_open_regset32,
717 .read = seq_read,
718 .llseek = seq_lseek,
719 .release = single_release,
720};
721
722/**
723 * debugfs_create_regset32 - create a debugfs file that returns register values
724 * @name: a pointer to a string containing the name of the file to create.
725 * @mode: the permission that the file should have
726 * @parent: a pointer to the parent dentry for this file. This should be a
727 * directory dentry if set. If this parameter is %NULL, then the
728 * file will be created in the root of the debugfs filesystem.
729 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
730 * to an array of register definitions, the array size and the base
731 * address where the register bank is to be found.
732 *
733 * This function creates a file in debugfs with the given name that reports
734 * the names and values of a set of 32-bit registers. If the @mode variable
735 * is so set it can be read from. Writing is not supported.
736 *
737 * This function will return a pointer to a dentry if it succeeds. This
738 * pointer must be passed to the debugfs_remove() function when the file is
739 * to be removed (no automatic cleanup happens if your module is unloaded,
740 * you are responsible here.) If an error occurs, %NULL will be returned.
741 *
742 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
743 * returned. It is not wise to check for this value, but rather, check for
744 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
745 * code.
746 */
88187398 747struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
1a087c6a
AR
748 struct dentry *parent,
749 struct debugfs_regset32 *regset)
750{
751 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
752}
753EXPORT_SYMBOL_GPL(debugfs_create_regset32);
3b85e4ab
HC
754
755#endif /* CONFIG_HAS_IOMEM */
98210b7f
AS
756
757struct debugfs_devm_entry {
758 int (*read)(struct seq_file *seq, void *data);
759 struct device *dev;
760};
761
762static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
763{
764 struct debugfs_devm_entry *entry = inode->i_private;
765
766 return single_open(f, entry->read, entry->dev);
767}
768
769static const struct file_operations debugfs_devm_entry_ops = {
770 .owner = THIS_MODULE,
771 .open = debugfs_devm_entry_open,
772 .release = single_release,
773 .read = seq_read,
774 .llseek = seq_lseek
775};
776
777/**
778 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
779 *
780 * @dev: device related to this debugfs file.
781 * @name: name of the debugfs file.
782 * @parent: a pointer to the parent dentry for this file. This should be a
783 * directory dentry if set. If this parameter is %NULL, then the
784 * file will be created in the root of the debugfs filesystem.
785 * @read_fn: function pointer called to print the seq_file content.
786 */
787struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
788 struct dentry *parent,
789 int (*read_fn)(struct seq_file *s,
790 void *data))
791{
792 struct debugfs_devm_entry *entry;
793
794 if (IS_ERR(parent))
795 return ERR_PTR(-ENOENT);
796
797 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
798 if (!entry)
799 return ERR_PTR(-ENOMEM);
800
801 entry->read = read_fn;
802 entry->dev = dev;
803
804 return debugfs_create_file(name, S_IRUGO, parent, entry,
805 &debugfs_devm_entry_ops);
806}
807EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
808