usb: musb: gadget: get rid of stop_activity()
[linux-2.6/btrfs-unstable.git] / fs / debugfs / file.c
blob517e6493843824f716b721845075b28d8e7d229d
1 /*
2 * file.c - part of debugfs, a tiny little debug file system
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 IBM Inc.
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.
11 * debugfs is for people to use instead of /proc or /sys.
12 * See Documentation/DocBook/filesystems for more details.
16 #include <linux/module.h>
17 #include <linux/fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/pagemap.h>
20 #include <linux/namei.h>
21 #include <linux/debugfs.h>
22 #include <linux/io.h>
23 #include <linux/slab.h>
24 #include <linux/atomic.h>
25 #include <linux/device.h>
27 static ssize_t default_read_file(struct file *file, char __user *buf,
28 size_t count, loff_t *ppos)
30 return 0;
33 static ssize_t default_write_file(struct file *file, const char __user *buf,
34 size_t count, loff_t *ppos)
36 return count;
39 const struct file_operations debugfs_file_operations = {
40 .read = default_read_file,
41 .write = default_write_file,
42 .open = simple_open,
43 .llseek = noop_llseek,
46 static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
48 nd_set_link(nd, dentry->d_inode->i_private);
49 return NULL;
52 const struct inode_operations debugfs_link_operations = {
53 .readlink = generic_readlink,
54 .follow_link = debugfs_follow_link,
57 static int debugfs_u8_set(void *data, u64 val)
59 *(u8 *)data = val;
60 return 0;
62 static int debugfs_u8_get(void *data, u64 *val)
64 *val = *(u8 *)data;
65 return 0;
67 DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
68 DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
69 DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
71 /**
72 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
73 * @name: a pointer to a string containing the name of the file to create.
74 * @mode: the permission that the file should have
75 * @parent: a pointer to the parent dentry for this file. This should be a
76 * directory dentry if set. If this parameter is %NULL, then the
77 * file will be created in the root of the debugfs filesystem.
78 * @value: a pointer to the variable that the file should read to and write
79 * from.
81 * This function creates a file in debugfs with the given name that
82 * contains the value of the variable @value. If the @mode variable is so
83 * set, it can be read from, and written to.
85 * This function will return a pointer to a dentry if it succeeds. This
86 * pointer must be passed to the debugfs_remove() function when the file is
87 * to be removed (no automatic cleanup happens if your module is unloaded,
88 * you are responsible here.) If an error occurs, %NULL will be returned.
90 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
91 * returned. It is not wise to check for this value, but rather, check for
92 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
93 * code.
95 struct dentry *debugfs_create_u8(const char *name, umode_t mode,
96 struct dentry *parent, u8 *value)
98 /* if there are no write bits set, make read only */
99 if (!(mode & S_IWUGO))
100 return debugfs_create_file(name, mode, parent, value, &fops_u8_ro);
101 /* if there are no read bits set, make write only */
102 if (!(mode & S_IRUGO))
103 return debugfs_create_file(name, mode, parent, value, &fops_u8_wo);
105 return debugfs_create_file(name, mode, parent, value, &fops_u8);
107 EXPORT_SYMBOL_GPL(debugfs_create_u8);
109 static int debugfs_u16_set(void *data, u64 val)
111 *(u16 *)data = val;
112 return 0;
114 static int debugfs_u16_get(void *data, u64 *val)
116 *val = *(u16 *)data;
117 return 0;
119 DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
120 DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
121 DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
124 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
125 * @name: a pointer to a string containing the name of the file to create.
126 * @mode: the permission that the file should have
127 * @parent: a pointer to the parent dentry for this file. This should be a
128 * directory dentry if set. If this parameter is %NULL, then the
129 * file will be created in the root of the debugfs filesystem.
130 * @value: a pointer to the variable that the file should read to and write
131 * from.
133 * This function creates a file in debugfs with the given name that
134 * contains the value of the variable @value. If the @mode variable is so
135 * set, it can be read from, and written to.
137 * This function will return a pointer to a dentry if it succeeds. This
138 * pointer must be passed to the debugfs_remove() function when the file is
139 * to be removed (no automatic cleanup happens if your module is unloaded,
140 * you are responsible here.) If an error occurs, %NULL will be returned.
142 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
143 * returned. It is not wise to check for this value, but rather, check for
144 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
145 * code.
147 struct dentry *debugfs_create_u16(const char *name, umode_t mode,
148 struct dentry *parent, u16 *value)
150 /* if there are no write bits set, make read only */
151 if (!(mode & S_IWUGO))
152 return debugfs_create_file(name, mode, parent, value, &fops_u16_ro);
153 /* if there are no read bits set, make write only */
154 if (!(mode & S_IRUGO))
155 return debugfs_create_file(name, mode, parent, value, &fops_u16_wo);
157 return debugfs_create_file(name, mode, parent, value, &fops_u16);
159 EXPORT_SYMBOL_GPL(debugfs_create_u16);
161 static int debugfs_u32_set(void *data, u64 val)
163 *(u32 *)data = val;
164 return 0;
166 static int debugfs_u32_get(void *data, u64 *val)
168 *val = *(u32 *)data;
169 return 0;
171 DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
172 DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
173 DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
176 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
177 * @name: a pointer to a string containing the name of the file to create.
178 * @mode: the permission that the file should have
179 * @parent: a pointer to the parent dentry for this file. This should be a
180 * directory dentry if set. If this parameter is %NULL, then the
181 * file will be created in the root of the debugfs filesystem.
182 * @value: a pointer to the variable that the file should read to and write
183 * from.
185 * This function creates a file in debugfs with the given name that
186 * contains the value of the variable @value. If the @mode variable is so
187 * set, it can be read from, and written to.
189 * This function will return a pointer to a dentry if it succeeds. This
190 * pointer must be passed to the debugfs_remove() function when the file is
191 * to be removed (no automatic cleanup happens if your module is unloaded,
192 * you are responsible here.) If an error occurs, %NULL will be returned.
194 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
195 * returned. It is not wise to check for this value, but rather, check for
196 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
197 * code.
199 struct dentry *debugfs_create_u32(const char *name, umode_t mode,
200 struct dentry *parent, u32 *value)
202 /* if there are no write bits set, make read only */
203 if (!(mode & S_IWUGO))
204 return debugfs_create_file(name, mode, parent, value, &fops_u32_ro);
205 /* if there are no read bits set, make write only */
206 if (!(mode & S_IRUGO))
207 return debugfs_create_file(name, mode, parent, value, &fops_u32_wo);
209 return debugfs_create_file(name, mode, parent, value, &fops_u32);
211 EXPORT_SYMBOL_GPL(debugfs_create_u32);
213 static int debugfs_u64_set(void *data, u64 val)
215 *(u64 *)data = val;
216 return 0;
219 static int debugfs_u64_get(void *data, u64 *val)
221 *val = *(u64 *)data;
222 return 0;
224 DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
225 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
226 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
229 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
230 * @name: a pointer to a string containing the name of the file to create.
231 * @mode: the permission that the file should have
232 * @parent: a pointer to the parent dentry for this file. This should be a
233 * directory dentry if set. If this parameter is %NULL, then the
234 * file will be created in the root of the debugfs filesystem.
235 * @value: a pointer to the variable that the file should read to and write
236 * from.
238 * This function creates a file in debugfs with the given name that
239 * contains the value of the variable @value. If the @mode variable is so
240 * set, it can be read from, and written to.
242 * This function will return a pointer to a dentry if it succeeds. This
243 * pointer must be passed to the debugfs_remove() function when the file is
244 * to be removed (no automatic cleanup happens if your module is unloaded,
245 * you are responsible here.) If an error occurs, %NULL will be returned.
247 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
248 * returned. It is not wise to check for this value, but rather, check for
249 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
250 * code.
252 struct dentry *debugfs_create_u64(const char *name, umode_t mode,
253 struct dentry *parent, u64 *value)
255 /* if there are no write bits set, make read only */
256 if (!(mode & S_IWUGO))
257 return debugfs_create_file(name, mode, parent, value, &fops_u64_ro);
258 /* if there are no read bits set, make write only */
259 if (!(mode & S_IRUGO))
260 return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);
262 return debugfs_create_file(name, mode, parent, value, &fops_u64);
264 EXPORT_SYMBOL_GPL(debugfs_create_u64);
266 DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
267 DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
268 DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
270 DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
271 DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
272 DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
274 DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
275 DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
276 DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
278 DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
281 * 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
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.
289 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
290 * @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.
298 struct dentry *debugfs_create_x8(const char *name, umode_t mode,
299 struct dentry *parent, u8 *value)
301 /* 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);
308 return debugfs_create_file(name, mode, parent, value, &fops_x8);
310 EXPORT_SYMBOL_GPL(debugfs_create_x8);
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.
322 struct dentry *debugfs_create_x16(const char *name, umode_t mode,
323 struct dentry *parent, u16 *value)
325 /* 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);
332 return debugfs_create_file(name, mode, parent, value, &fops_x16);
334 EXPORT_SYMBOL_GPL(debugfs_create_x16);
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.
346 struct dentry *debugfs_create_x32(const char *name, umode_t mode,
347 struct dentry *parent, u32 *value)
349 /* 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);
356 return debugfs_create_file(name, mode, parent, value, &fops_x32);
358 EXPORT_SYMBOL_GPL(debugfs_create_x32);
361 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
362 * @name: a pointer to a string containing the name of the file to create.
363 * @mode: the permission that the file should have
364 * @parent: a pointer to the parent dentry for this file. This should be a
365 * directory dentry if set. If this parameter is %NULL, then the
366 * file will be created in the root of the debugfs filesystem.
367 * @value: a pointer to the variable that the file should read to and write
368 * from.
370 struct dentry *debugfs_create_x64(const char *name, umode_t mode,
371 struct dentry *parent, u64 *value)
373 return debugfs_create_file(name, mode, parent, value, &fops_x64);
375 EXPORT_SYMBOL_GPL(debugfs_create_x64);
378 static int debugfs_size_t_set(void *data, u64 val)
380 *(size_t *)data = val;
381 return 0;
383 static int debugfs_size_t_get(void *data, u64 *val)
385 *val = *(size_t *)data;
386 return 0;
388 DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
389 "%llu\n"); /* %llu and %zu are more or less the same */
392 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
393 * @name: a pointer to a string containing the name of the file to create.
394 * @mode: the permission that the file should have
395 * @parent: a pointer to the parent dentry for this file. This should be a
396 * directory dentry if set. If this parameter is %NULL, then the
397 * file will be created in the root of the debugfs filesystem.
398 * @value: a pointer to the variable that the file should read to and write
399 * from.
401 struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
402 struct dentry *parent, size_t *value)
404 return debugfs_create_file(name, mode, parent, value, &fops_size_t);
406 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
408 static int debugfs_atomic_t_set(void *data, u64 val)
410 atomic_set((atomic_t *)data, val);
411 return 0;
413 static int debugfs_atomic_t_get(void *data, u64 *val)
415 *val = atomic_read((atomic_t *)data);
416 return 0;
418 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
419 debugfs_atomic_t_set, "%lld\n");
420 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
421 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
424 * debugfs_create_atomic_t - create a debugfs file that is used to read and
425 * write an atomic_t value
426 * @name: a pointer to a string containing the name of the file to create.
427 * @mode: the permission that the file should have
428 * @parent: a pointer to the parent dentry for this file. This should be a
429 * directory dentry if set. If this parameter is %NULL, then the
430 * file will be created in the root of the debugfs filesystem.
431 * @value: a pointer to the variable that the file should read to and write
432 * from.
434 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
435 struct dentry *parent, atomic_t *value)
437 /* if there are no write bits set, make read only */
438 if (!(mode & S_IWUGO))
439 return debugfs_create_file(name, mode, parent, value,
440 &fops_atomic_t_ro);
441 /* if there are no read bits set, make write only */
442 if (!(mode & S_IRUGO))
443 return debugfs_create_file(name, mode, parent, value,
444 &fops_atomic_t_wo);
446 return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
448 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
450 static ssize_t read_file_bool(struct file *file, char __user *user_buf,
451 size_t count, loff_t *ppos)
453 char buf[3];
454 u32 *val = file->private_data;
456 if (*val)
457 buf[0] = 'Y';
458 else
459 buf[0] = 'N';
460 buf[1] = '\n';
461 buf[2] = 0x00;
462 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
465 static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
466 size_t count, loff_t *ppos)
468 char buf[32];
469 size_t buf_size;
470 bool bv;
471 u32 *val = file->private_data;
473 buf_size = min(count, (sizeof(buf)-1));
474 if (copy_from_user(buf, user_buf, buf_size))
475 return -EFAULT;
477 buf[buf_size] = '\0';
478 if (strtobool(buf, &bv) == 0)
479 *val = bv;
481 return count;
484 static const struct file_operations fops_bool = {
485 .read = read_file_bool,
486 .write = write_file_bool,
487 .open = simple_open,
488 .llseek = default_llseek,
492 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
493 * @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
496 * directory dentry if set. If this parameter is %NULL, then the
497 * file will be created in the root of the debugfs filesystem.
498 * @value: a pointer to the variable that the file should read to and write
499 * from.
501 * This function creates a file in debugfs with the given name that
502 * contains the value of the variable @value. If the @mode variable is so
503 * set, it can be read from, and written to.
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,
508 * you are responsible here.) If an error occurs, %NULL will be returned.
510 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
511 * returned. It is not wise to check for this value, but rather, check for
512 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
513 * code.
515 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
516 struct dentry *parent, u32 *value)
518 return debugfs_create_file(name, mode, parent, value, &fops_bool);
520 EXPORT_SYMBOL_GPL(debugfs_create_bool);
522 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
523 size_t count, loff_t *ppos)
525 struct debugfs_blob_wrapper *blob = file->private_data;
526 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
527 blob->size);
530 static const struct file_operations fops_blob = {
531 .read = read_file_blob,
532 .open = simple_open,
533 .llseek = default_llseek,
537 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
538 * @name: a pointer to a string containing the name of the file to create.
539 * @mode: the permission that the file should have
540 * @parent: a pointer to the parent dentry for this file. This should be a
541 * directory dentry if set. If this parameter is %NULL, then the
542 * file will be created in the root of the debugfs filesystem.
543 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
544 * to the blob data and the size of the data.
546 * This function creates a file in debugfs with the given name that exports
547 * @blob->data as a binary blob. If the @mode variable is so set it can be
548 * read from. Writing is not supported.
550 * This function will return a pointer to a dentry if it succeeds. This
551 * pointer must be passed to the debugfs_remove() function when the file is
552 * to be removed (no automatic cleanup happens if your module is unloaded,
553 * you are responsible here.) If an error occurs, %NULL will be returned.
555 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
556 * returned. It is not wise to check for this value, but rather, check for
557 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
558 * code.
560 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
561 struct dentry *parent,
562 struct debugfs_blob_wrapper *blob)
564 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
566 EXPORT_SYMBOL_GPL(debugfs_create_blob);
568 struct array_data {
569 void *array;
570 u32 elements;
573 static size_t u32_format_array(char *buf, size_t bufsize,
574 u32 *array, int array_size)
576 size_t ret = 0;
578 while (--array_size >= 0) {
579 size_t len;
580 char term = array_size ? ' ' : '\n';
582 len = snprintf(buf, bufsize, "%u%c", *array++, term);
583 ret += len;
585 buf += len;
586 bufsize -= len;
588 return ret;
591 static int u32_array_open(struct inode *inode, struct file *file)
593 struct array_data *data = inode->i_private;
594 int size, elements = data->elements;
595 char *buf;
598 * Max size:
599 * - 10 digits + ' '/'\n' = 11 bytes per number
600 * - terminating NUL character
602 size = elements*11;
603 buf = kmalloc(size+1, GFP_KERNEL);
604 if (!buf)
605 return -ENOMEM;
606 buf[size] = 0;
608 file->private_data = buf;
609 u32_format_array(buf, size, data->array, data->elements);
611 return nonseekable_open(inode, file);
614 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
615 loff_t *ppos)
617 size_t size = strlen(file->private_data);
619 return simple_read_from_buffer(buf, len, ppos,
620 file->private_data, size);
623 static int u32_array_release(struct inode *inode, struct file *file)
625 kfree(file->private_data);
627 return 0;
630 static const struct file_operations u32_array_fops = {
631 .owner = THIS_MODULE,
632 .open = u32_array_open,
633 .release = u32_array_release,
634 .read = u32_array_read,
635 .llseek = no_llseek,
639 * debugfs_create_u32_array - create a debugfs file that is used to read u32
640 * array.
641 * @name: a pointer to a string containing the name of the file to create.
642 * @mode: the permission that the file should have.
643 * @parent: a pointer to the parent dentry for this file. This should be a
644 * directory dentry if set. If this parameter is %NULL, then the
645 * file will be created in the root of the debugfs filesystem.
646 * @array: u32 array that provides data.
647 * @elements: total number of elements in the array.
649 * This function creates a file in debugfs with the given name that exports
650 * @array as data. If the @mode variable is so set it can be read from.
651 * Writing is not supported. Seek within the file is also not supported.
652 * Once array is created its size can not be changed.
654 * The function returns a pointer to dentry on success. If debugfs is not
655 * enabled in the kernel, the value -%ENODEV will be returned.
657 struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
658 struct dentry *parent,
659 u32 *array, u32 elements)
661 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
663 if (data == NULL)
664 return NULL;
666 data->array = array;
667 data->elements = elements;
669 return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
671 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
673 #ifdef CONFIG_HAS_IOMEM
676 * The regset32 stuff is used to print 32-bit registers using the
677 * seq_file utilities. We offer printing a register set in an already-opened
678 * sequential file or create a debugfs file that only prints a regset32.
682 * debugfs_print_regs32 - use seq_print to describe a set of registers
683 * @s: the seq_file structure being used to generate output
684 * @regs: an array if struct debugfs_reg32 structures
685 * @nregs: the length of the above array
686 * @base: the base address to be used in reading the registers
687 * @prefix: a string to be prefixed to every output line
689 * This function outputs a text block describing the current values of
690 * some 32-bit hardware registers. It is meant to be used within debugfs
691 * files based on seq_file that need to show registers, intermixed with other
692 * information. The prefix argument may be used to specify a leading string,
693 * because some peripherals have several blocks of identical registers,
694 * for example configuration of dma channels
696 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
697 int nregs, void __iomem *base, char *prefix)
699 int i;
701 for (i = 0; i < nregs; i++, regs++) {
702 if (prefix)
703 seq_printf(s, "%s", prefix);
704 seq_printf(s, "%s = 0x%08x\n", regs->name,
705 readl(base + regs->offset));
706 if (seq_has_overflowed(s))
707 break;
710 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
712 static int debugfs_show_regset32(struct seq_file *s, void *data)
714 struct debugfs_regset32 *regset = s->private;
716 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
717 return 0;
720 static int debugfs_open_regset32(struct inode *inode, struct file *file)
722 return single_open(file, debugfs_show_regset32, inode->i_private);
725 static const struct file_operations fops_regset32 = {
726 .open = debugfs_open_regset32,
727 .read = seq_read,
728 .llseek = seq_lseek,
729 .release = single_release,
733 * debugfs_create_regset32 - create a debugfs file that returns register values
734 * @name: a pointer to a string containing the name of the file to create.
735 * @mode: the permission that the file should have
736 * @parent: a pointer to the parent dentry for this file. This should be a
737 * directory dentry if set. If this parameter is %NULL, then the
738 * file will be created in the root of the debugfs filesystem.
739 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
740 * to an array of register definitions, the array size and the base
741 * address where the register bank is to be found.
743 * This function creates a file in debugfs with the given name that reports
744 * the names and values of a set of 32-bit registers. If the @mode variable
745 * is so set it can be read from. Writing is not supported.
747 * This function will return a pointer to a dentry if it succeeds. This
748 * pointer must be passed to the debugfs_remove() function when the file is
749 * to be removed (no automatic cleanup happens if your module is unloaded,
750 * you are responsible here.) If an error occurs, %NULL will be returned.
752 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
753 * returned. It is not wise to check for this value, but rather, check for
754 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
755 * code.
757 struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
758 struct dentry *parent,
759 struct debugfs_regset32 *regset)
761 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
763 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
765 #endif /* CONFIG_HAS_IOMEM */
767 struct debugfs_devm_entry {
768 int (*read)(struct seq_file *seq, void *data);
769 struct device *dev;
772 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
774 struct debugfs_devm_entry *entry = inode->i_private;
776 return single_open(f, entry->read, entry->dev);
779 static const struct file_operations debugfs_devm_entry_ops = {
780 .owner = THIS_MODULE,
781 .open = debugfs_devm_entry_open,
782 .release = single_release,
783 .read = seq_read,
784 .llseek = seq_lseek
788 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
790 * @dev: device related to this debugfs file.
791 * @name: name of the debugfs file.
792 * @parent: a pointer to the parent dentry for this file. This should be a
793 * directory dentry if set. If this parameter is %NULL, then the
794 * file will be created in the root of the debugfs filesystem.
795 * @read_fn: function pointer called to print the seq_file content.
797 struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
798 struct dentry *parent,
799 int (*read_fn)(struct seq_file *s,
800 void *data))
802 struct debugfs_devm_entry *entry;
804 if (IS_ERR(parent))
805 return ERR_PTR(-ENOENT);
807 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
808 if (!entry)
809 return ERR_PTR(-ENOMEM);
811 entry->read = read_fn;
812 entry->dev = dev;
814 return debugfs_create_file(name, S_IRUGO, parent, entry,
815 &debugfs_devm_entry_ops);
817 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);