xref: /openbmc/linux/fs/debugfs/file.c (revision 8c0b9ee8)
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.
12  *  See Documentation/DocBook/filesystems for more details.
13  *
14  */
15 
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>
26 
27 static ssize_t default_read_file(struct file *file, char __user *buf,
28 				 size_t count, loff_t *ppos)
29 {
30 	return 0;
31 }
32 
33 static ssize_t default_write_file(struct file *file, const char __user *buf,
34 				   size_t count, loff_t *ppos)
35 {
36 	return count;
37 }
38 
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,
44 };
45 
46 static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
47 {
48 	nd_set_link(nd, dentry->d_inode->i_private);
49 	return NULL;
50 }
51 
52 const struct inode_operations debugfs_link_operations = {
53 	.readlink       = generic_readlink,
54 	.follow_link    = debugfs_follow_link,
55 };
56 
57 static int debugfs_u8_set(void *data, u64 val)
58 {
59 	*(u8 *)data = val;
60 	return 0;
61 }
62 static int debugfs_u8_get(void *data, u64 *val)
63 {
64 	*val = *(u8 *)data;
65 	return 0;
66 }
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");
70 
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.
80  *
81  * This function creates a file in debugfs with the given name that
82  * contains the value of the variable @value.  If the @mode variable is so
83  * set, it can be read from, and written to.
84  *
85  * This function will return a pointer to a dentry if it succeeds.  This
86  * pointer must be passed to the debugfs_remove() function when the file is
87  * to be removed (no automatic cleanup happens if your module is unloaded,
88  * you are responsible here.)  If an error occurs, %NULL will be returned.
89  *
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.
94  */
95 struct dentry *debugfs_create_u8(const char *name, umode_t mode,
96 				 struct dentry *parent, u8 *value)
97 {
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);
104 
105 	return debugfs_create_file(name, mode, parent, value, &fops_u8);
106 }
107 EXPORT_SYMBOL_GPL(debugfs_create_u8);
108 
109 static int debugfs_u16_set(void *data, u64 val)
110 {
111 	*(u16 *)data = val;
112 	return 0;
113 }
114 static int debugfs_u16_get(void *data, u64 *val)
115 {
116 	*val = *(u16 *)data;
117 	return 0;
118 }
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");
122 
123 /**
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.
132  *
133  * This function creates a file in debugfs with the given name that
134  * contains the value of the variable @value.  If the @mode variable is so
135  * set, it can be read from, and written to.
136  *
137  * This function will return a pointer to a dentry if it succeeds.  This
138  * pointer must be passed to the debugfs_remove() function when the file is
139  * to be removed (no automatic cleanup happens if your module is unloaded,
140  * you are responsible here.)  If an error occurs, %NULL will be returned.
141  *
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.
146  */
147 struct dentry *debugfs_create_u16(const char *name, umode_t mode,
148 				  struct dentry *parent, u16 *value)
149 {
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);
156 
157 	return debugfs_create_file(name, mode, parent, value, &fops_u16);
158 }
159 EXPORT_SYMBOL_GPL(debugfs_create_u16);
160 
161 static int debugfs_u32_set(void *data, u64 val)
162 {
163 	*(u32 *)data = val;
164 	return 0;
165 }
166 static int debugfs_u32_get(void *data, u64 *val)
167 {
168 	*val = *(u32 *)data;
169 	return 0;
170 }
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");
174 
175 /**
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.
184  *
185  * This function creates a file in debugfs with the given name that
186  * contains the value of the variable @value.  If the @mode variable is so
187  * set, it can be read from, and written to.
188  *
189  * This function will return a pointer to a dentry if it succeeds.  This
190  * pointer must be passed to the debugfs_remove() function when the file is
191  * to be removed (no automatic cleanup happens if your module is unloaded,
192  * you are responsible here.)  If an error occurs, %NULL will be returned.
193  *
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.
198  */
199 struct dentry *debugfs_create_u32(const char *name, umode_t mode,
200 				 struct dentry *parent, u32 *value)
201 {
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);
208 
209 	return debugfs_create_file(name, mode, parent, value, &fops_u32);
210 }
211 EXPORT_SYMBOL_GPL(debugfs_create_u32);
212 
213 static int debugfs_u64_set(void *data, u64 val)
214 {
215 	*(u64 *)data = val;
216 	return 0;
217 }
218 
219 static int debugfs_u64_get(void *data, u64 *val)
220 {
221 	*val = *(u64 *)data;
222 	return 0;
223 }
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");
227 
228 /**
229  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
230  * @name: a pointer to a string containing the name of the file to create.
231  * @mode: the permission that the file should have
232  * @parent: a pointer to the parent dentry for this file.  This should be a
233  *          directory dentry if set.  If this parameter is %NULL, then the
234  *          file will be created in the root of the debugfs filesystem.
235  * @value: a pointer to the variable that the file should read to and write
236  *         from.
237  *
238  * This function creates a file in debugfs with the given name that
239  * contains the value of the variable @value.  If the @mode variable is so
240  * set, it can be read from, and written to.
241  *
242  * This function will return a pointer to a dentry if it succeeds.  This
243  * pointer must be passed to the debugfs_remove() function when the file is
244  * to be removed (no automatic cleanup happens if your module is unloaded,
245  * you are responsible here.)  If an error occurs, %NULL will be returned.
246  *
247  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
248  * returned.  It is not wise to check for this value, but rather, check for
249  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
250  * code.
251  */
252 struct dentry *debugfs_create_u64(const char *name, umode_t mode,
253 				 struct dentry *parent, u64 *value)
254 {
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);
261 
262 	return debugfs_create_file(name, mode, parent, value, &fops_u64);
263 }
264 EXPORT_SYMBOL_GPL(debugfs_create_u64);
265 
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");
269 
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");
273 
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");
277 
278 DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
279 
280 /*
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
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 
288 /**
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.
297  */
298 struct dentry *debugfs_create_x8(const char *name, umode_t mode,
299 				 struct dentry *parent, u8 *value)
300 {
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);
307 
308 	return debugfs_create_file(name, mode, parent, value, &fops_x8);
309 }
310 EXPORT_SYMBOL_GPL(debugfs_create_x8);
311 
312 /**
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  */
322 struct dentry *debugfs_create_x16(const char *name, umode_t mode,
323 				 struct dentry *parent, u16 *value)
324 {
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);
331 
332 	return debugfs_create_file(name, mode, parent, value, &fops_x16);
333 }
334 EXPORT_SYMBOL_GPL(debugfs_create_x16);
335 
336 /**
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  */
346 struct dentry *debugfs_create_x32(const char *name, umode_t mode,
347 				 struct dentry *parent, u32 *value)
348 {
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);
355 
356 	return debugfs_create_file(name, mode, parent, value, &fops_x32);
357 }
358 EXPORT_SYMBOL_GPL(debugfs_create_x32);
359 
360 /**
361  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
362  * @name: a pointer to a string containing the name of the file to create.
363  * @mode: the permission that the file should have
364  * @parent: a pointer to the parent dentry for this file.  This should be a
365  *          directory dentry if set.  If this parameter is %NULL, then the
366  *          file will be created in the root of the debugfs filesystem.
367  * @value: a pointer to the variable that the file should read to and write
368  *         from.
369  */
370 struct dentry *debugfs_create_x64(const char *name, umode_t mode,
371 				 struct dentry *parent, u64 *value)
372 {
373 	return debugfs_create_file(name, mode, parent, value, &fops_x64);
374 }
375 EXPORT_SYMBOL_GPL(debugfs_create_x64);
376 
377 
378 static int debugfs_size_t_set(void *data, u64 val)
379 {
380 	*(size_t *)data = val;
381 	return 0;
382 }
383 static int debugfs_size_t_get(void *data, u64 *val)
384 {
385 	*val = *(size_t *)data;
386 	return 0;
387 }
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 */
390 
391 /**
392  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
393  * @name: a pointer to a string containing the name of the file to create.
394  * @mode: the permission that the file should have
395  * @parent: a pointer to the parent dentry for this file.  This should be a
396  *          directory dentry if set.  If this parameter is %NULL, then the
397  *          file will be created in the root of the debugfs filesystem.
398  * @value: a pointer to the variable that the file should read to and write
399  *         from.
400  */
401 struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
402 				     struct dentry *parent, size_t *value)
403 {
404 	return debugfs_create_file(name, mode, parent, value, &fops_size_t);
405 }
406 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
407 
408 static int debugfs_atomic_t_set(void *data, u64 val)
409 {
410 	atomic_set((atomic_t *)data, val);
411 	return 0;
412 }
413 static int debugfs_atomic_t_get(void *data, u64 *val)
414 {
415 	*val = atomic_read((atomic_t *)data);
416 	return 0;
417 }
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");
422 
423 /**
424  * debugfs_create_atomic_t - create a debugfs file that is used to read and
425  * write an atomic_t value
426  * @name: a pointer to a string containing the name of the file to create.
427  * @mode: the permission that the file should have
428  * @parent: a pointer to the parent dentry for this file.  This should be a
429  *          directory dentry if set.  If this parameter is %NULL, then the
430  *          file will be created in the root of the debugfs filesystem.
431  * @value: a pointer to the variable that the file should read to and write
432  *         from.
433  */
434 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
435 				 struct dentry *parent, atomic_t *value)
436 {
437 	/* if there are no write bits set, make read only */
438 	if (!(mode & S_IWUGO))
439 		return debugfs_create_file(name, mode, parent, value,
440 					&fops_atomic_t_ro);
441 	/* if there are no read bits set, make write only */
442 	if (!(mode & S_IRUGO))
443 		return debugfs_create_file(name, mode, parent, value,
444 					&fops_atomic_t_wo);
445 
446 	return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
447 }
448 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
449 
450 static ssize_t read_file_bool(struct file *file, char __user *user_buf,
451 			      size_t count, loff_t *ppos)
452 {
453 	char buf[3];
454 	u32 *val = file->private_data;
455 
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);
463 }
464 
465 static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
466 			       size_t count, loff_t *ppos)
467 {
468 	char buf[32];
469 	size_t buf_size;
470 	bool bv;
471 	u32 *val = file->private_data;
472 
473 	buf_size = min(count, (sizeof(buf)-1));
474 	if (copy_from_user(buf, user_buf, buf_size))
475 		return -EFAULT;
476 
477 	buf[buf_size] = '\0';
478 	if (strtobool(buf, &bv) == 0)
479 		*val = bv;
480 
481 	return count;
482 }
483 
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,
489 };
490 
491 /**
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.
500  *
501  * This function creates a file in debugfs with the given name that
502  * contains the value of the variable @value.  If the @mode variable is so
503  * set, it can be read from, and written to.
504  *
505  * This function will return a pointer to a dentry if it succeeds.  This
506  * pointer must be passed to the debugfs_remove() function when the file is
507  * to be removed (no automatic cleanup happens if your module is unloaded,
508  * you are responsible here.)  If an error occurs, %NULL will be returned.
509  *
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.
514  */
515 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
516 				   struct dentry *parent, u32 *value)
517 {
518 	return debugfs_create_file(name, mode, parent, value, &fops_bool);
519 }
520 EXPORT_SYMBOL_GPL(debugfs_create_bool);
521 
522 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
523 			      size_t count, loff_t *ppos)
524 {
525 	struct debugfs_blob_wrapper *blob = file->private_data;
526 	return simple_read_from_buffer(user_buf, count, ppos, blob->data,
527 			blob->size);
528 }
529 
530 static const struct file_operations fops_blob = {
531 	.read =		read_file_blob,
532 	.open =		simple_open,
533 	.llseek =	default_llseek,
534 };
535 
536 /**
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.
545  *
546  * This function creates a file in debugfs with the given name that exports
547  * @blob->data as a binary blob. If the @mode variable is so set it can be
548  * read from. Writing is not supported.
549  *
550  * This function will return a pointer to a dentry if it succeeds.  This
551  * pointer must be passed to the debugfs_remove() function when the file is
552  * to be removed (no automatic cleanup happens if your module is unloaded,
553  * you are responsible here.)  If an error occurs, %NULL will be returned.
554  *
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.
559  */
560 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
561 				   struct dentry *parent,
562 				   struct debugfs_blob_wrapper *blob)
563 {
564 	return debugfs_create_file(name, mode, parent, blob, &fops_blob);
565 }
566 EXPORT_SYMBOL_GPL(debugfs_create_blob);
567 
568 struct array_data {
569 	void *array;
570 	u32 elements;
571 };
572 
573 static size_t u32_format_array(char *buf, size_t bufsize,
574 			       u32 *array, int array_size)
575 {
576 	size_t ret = 0;
577 
578 	while (--array_size >= 0) {
579 		size_t len;
580 		char term = array_size ? ' ' : '\n';
581 
582 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
583 		ret += len;
584 
585 		buf += len;
586 		bufsize -= len;
587 	}
588 	return ret;
589 }
590 
591 static int u32_array_open(struct inode *inode, struct file *file)
592 {
593 	struct array_data *data = inode->i_private;
594 	int size, elements = data->elements;
595 	char *buf;
596 
597 	/*
598 	 * Max size:
599 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
600 	 *  - terminating NUL character
601 	 */
602 	size = elements*11;
603 	buf = kmalloc(size+1, GFP_KERNEL);
604 	if (!buf)
605 		return -ENOMEM;
606 	buf[size] = 0;
607 
608 	file->private_data = buf;
609 	u32_format_array(buf, size, data->array, data->elements);
610 
611 	return nonseekable_open(inode, file);
612 }
613 
614 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
615 			      loff_t *ppos)
616 {
617 	size_t size = strlen(file->private_data);
618 
619 	return simple_read_from_buffer(buf, len, ppos,
620 					file->private_data, size);
621 }
622 
623 static int u32_array_release(struct inode *inode, struct file *file)
624 {
625 	kfree(file->private_data);
626 
627 	return 0;
628 }
629 
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,
636 };
637 
638 /**
639  * debugfs_create_u32_array - create a debugfs file that is used to read u32
640  * array.
641  * @name: a pointer to a string containing the name of the file to create.
642  * @mode: the permission that the file should have.
643  * @parent: a pointer to the parent dentry for this file.  This should be a
644  *          directory dentry if set.  If this parameter is %NULL, then the
645  *          file will be created in the root of the debugfs filesystem.
646  * @array: u32 array that provides data.
647  * @elements: total number of elements in the array.
648  *
649  * This function creates a file in debugfs with the given name that exports
650  * @array as data. If the @mode variable is so set it can be read from.
651  * Writing is not supported. Seek within the file is also not supported.
652  * Once array is created its size can not be changed.
653  *
654  * The function returns a pointer to dentry on success. If debugfs is not
655  * enabled in the kernel, the value -%ENODEV will be returned.
656  */
657 struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
658 					    struct dentry *parent,
659 					    u32 *array, u32 elements)
660 {
661 	struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
662 
663 	if (data == NULL)
664 		return NULL;
665 
666 	data->array = array;
667 	data->elements = elements;
668 
669 	return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
670 }
671 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
672 
673 #ifdef CONFIG_HAS_IOMEM
674 
675 /*
676  * The regset32 stuff is used to print 32-bit registers using the
677  * seq_file utilities. We offer printing a register set in an already-opened
678  * sequential file or create a debugfs file that only prints a regset32.
679  */
680 
681 /**
682  * debugfs_print_regs32 - use seq_print to describe a set of registers
683  * @s: the seq_file structure being used to generate output
684  * @regs: an array if struct debugfs_reg32 structures
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
688  *
689  * This function outputs a text block describing the current values of
690  * some 32-bit hardware registers. It is meant to be used within debugfs
691  * files based on seq_file that need to show registers, intermixed with other
692  * information. The prefix argument may be used to specify a leading string,
693  * because some peripherals have several blocks of identical registers,
694  * for example configuration of dma channels
695  */
696 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
697 			  int nregs, void __iomem *base, char *prefix)
698 {
699 	int i;
700 
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;
708 	}
709 }
710 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
711 
712 static int debugfs_show_regset32(struct seq_file *s, void *data)
713 {
714 	struct debugfs_regset32 *regset = s->private;
715 
716 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
717 	return 0;
718 }
719 
720 static int debugfs_open_regset32(struct inode *inode, struct file *file)
721 {
722 	return single_open(file, debugfs_show_regset32, inode->i_private);
723 }
724 
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,
730 };
731 
732 /**
733  * debugfs_create_regset32 - create a debugfs file that returns register values
734  * @name: a pointer to a string containing the name of the file to create.
735  * @mode: the permission that the file should have
736  * @parent: a pointer to the parent dentry for this file.  This should be a
737  *          directory dentry if set.  If this parameter is %NULL, then the
738  *          file will be created in the root of the debugfs filesystem.
739  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
740  *          to an array of register definitions, the array size and the base
741  *          address where the register bank is to be found.
742  *
743  * This function creates a file in debugfs with the given name that reports
744  * the names and values of a set of 32-bit registers. If the @mode variable
745  * is so set it can be read from. Writing is not supported.
746  *
747  * This function will return a pointer to a dentry if it succeeds.  This
748  * pointer must be passed to the debugfs_remove() function when the file is
749  * to be removed (no automatic cleanup happens if your module is unloaded,
750  * you are responsible here.)  If an error occurs, %NULL will be returned.
751  *
752  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
753  * returned.  It is not wise to check for this value, but rather, check for
754  * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
755  * code.
756  */
757 struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
758 				       struct dentry *parent,
759 				       struct debugfs_regset32 *regset)
760 {
761 	return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
762 }
763 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
764 
765 #endif /* CONFIG_HAS_IOMEM */
766 
767 struct debugfs_devm_entry {
768 	int (*read)(struct seq_file *seq, void *data);
769 	struct device *dev;
770 };
771 
772 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
773 {
774 	struct debugfs_devm_entry *entry = inode->i_private;
775 
776 	return single_open(f, entry->read, entry->dev);
777 }
778 
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
785 };
786 
787 /**
788  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
789  *
790  * @dev: device related to this debugfs file.
791  * @name: name of the debugfs file.
792  * @parent: a pointer to the parent dentry for this file.  This should be a
793  *	directory dentry if set.  If this parameter is %NULL, then the
794  *	file will be created in the root of the debugfs filesystem.
795  * @read_fn: function pointer called to print the seq_file content.
796  */
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))
801 {
802 	struct debugfs_devm_entry *entry;
803 
804 	if (IS_ERR(parent))
805 		return ERR_PTR(-ENOENT);
806 
807 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
808 	if (!entry)
809 		return ERR_PTR(-ENOMEM);
810 
811 	entry->read = read_fn;
812 	entry->dev = dev;
813 
814 	return debugfs_create_file(name, S_IRUGO, parent, entry,
815 				   &debugfs_devm_entry_ops);
816 }
817 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
818 
819