Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux...
[deliverable/linux.git] / fs / debugfs / file.c
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/debugfs.h>
21 #include <linux/io.h>
22 #include <linux/slab.h>
23 #include <linux/atomic.h>
24 #include <linux/device.h>
25
26 static ssize_t default_read_file(struct file *file, char __user *buf,
27 size_t count, loff_t *ppos)
28 {
29 return 0;
30 }
31
32 static 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
38 const struct file_operations debugfs_file_operations = {
39 .read = default_read_file,
40 .write = default_write_file,
41 .open = simple_open,
42 .llseek = noop_llseek,
43 };
44
45 static int debugfs_u8_set(void *data, u64 val)
46 {
47 *(u8 *)data = val;
48 return 0;
49 }
50 static int debugfs_u8_get(void *data, u64 *val)
51 {
52 *val = *(u8 *)data;
53 return 0;
54 }
55 DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
56 DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
57 DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
58
59 /**
60 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
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
64 * directory dentry if set. If this parameter is %NULL, then the
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,
76 * you are responsible here.) If an error occurs, %NULL will be returned.
77 *
78 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
79 * returned. It is not wise to check for this value, but rather, check for
80 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
81 * code.
82 */
83 struct dentry *debugfs_create_u8(const char *name, umode_t mode,
84 struct dentry *parent, u8 *value)
85 {
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
93 return debugfs_create_file(name, mode, parent, value, &fops_u8);
94 }
95 EXPORT_SYMBOL_GPL(debugfs_create_u8);
96
97 static int debugfs_u16_set(void *data, u64 val)
98 {
99 *(u16 *)data = val;
100 return 0;
101 }
102 static int debugfs_u16_get(void *data, u64 *val)
103 {
104 *val = *(u16 *)data;
105 return 0;
106 }
107 DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
108 DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
109 DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
110
111 /**
112 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
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
116 * directory dentry if set. If this parameter is %NULL, then the
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,
128 * you are responsible here.) If an error occurs, %NULL will be returned.
129 *
130 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
131 * returned. It is not wise to check for this value, but rather, check for
132 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
133 * code.
134 */
135 struct dentry *debugfs_create_u16(const char *name, umode_t mode,
136 struct dentry *parent, u16 *value)
137 {
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
145 return debugfs_create_file(name, mode, parent, value, &fops_u16);
146 }
147 EXPORT_SYMBOL_GPL(debugfs_create_u16);
148
149 static int debugfs_u32_set(void *data, u64 val)
150 {
151 *(u32 *)data = val;
152 return 0;
153 }
154 static int debugfs_u32_get(void *data, u64 *val)
155 {
156 *val = *(u32 *)data;
157 return 0;
158 }
159 DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
160 DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
161 DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
162
163 /**
164 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
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
168 * directory dentry if set. If this parameter is %NULL, then the
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,
180 * you are responsible here.) If an error occurs, %NULL will be returned.
181 *
182 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
183 * returned. It is not wise to check for this value, but rather, check for
184 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
185 * code.
186 */
187 struct dentry *debugfs_create_u32(const char *name, umode_t mode,
188 struct dentry *parent, u32 *value)
189 {
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
197 return debugfs_create_file(name, mode, parent, value, &fops_u32);
198 }
199 EXPORT_SYMBOL_GPL(debugfs_create_u32);
200
201 static int debugfs_u64_set(void *data, u64 val)
202 {
203 *(u64 *)data = val;
204 return 0;
205 }
206
207 static int debugfs_u64_get(void *data, u64 *val)
208 {
209 *val = *(u64 *)data;
210 return 0;
211 }
212 DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
213 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
214 DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
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 */
240 struct dentry *debugfs_create_u64(const char *name, umode_t mode,
241 struct dentry *parent, u64 *value)
242 {
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
250 return debugfs_create_file(name, mode, parent, value, &fops_u64);
251 }
252 EXPORT_SYMBOL_GPL(debugfs_create_u64);
253
254 DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
255 DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
256 DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
257
258 DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
259 DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
260 DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
261
262 DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
263 DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
264 DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
265
266 DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
267
268 /*
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
270 *
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
273 * decimal functions.
274 */
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 */
286 struct dentry *debugfs_create_x8(const char *name, umode_t mode,
287 struct dentry *parent, u8 *value)
288 {
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
296 return debugfs_create_file(name, mode, parent, value, &fops_x8);
297 }
298 EXPORT_SYMBOL_GPL(debugfs_create_x8);
299
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 */
310 struct dentry *debugfs_create_x16(const char *name, umode_t mode,
311 struct dentry *parent, u16 *value)
312 {
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
320 return debugfs_create_file(name, mode, parent, value, &fops_x16);
321 }
322 EXPORT_SYMBOL_GPL(debugfs_create_x16);
323
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 */
334 struct dentry *debugfs_create_x32(const char *name, umode_t mode,
335 struct dentry *parent, u32 *value)
336 {
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
344 return debugfs_create_file(name, mode, parent, value, &fops_x32);
345 }
346 EXPORT_SYMBOL_GPL(debugfs_create_x32);
347
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 */
358 struct dentry *debugfs_create_x64(const char *name, umode_t mode,
359 struct dentry *parent, u64 *value)
360 {
361 return debugfs_create_file(name, mode, parent, value, &fops_x64);
362 }
363 EXPORT_SYMBOL_GPL(debugfs_create_x64);
364
365
366 static int debugfs_size_t_set(void *data, u64 val)
367 {
368 *(size_t *)data = val;
369 return 0;
370 }
371 static int debugfs_size_t_get(void *data, u64 *val)
372 {
373 *val = *(size_t *)data;
374 return 0;
375 }
376 DEFINE_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 */
389 struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
390 struct dentry *parent, size_t *value)
391 {
392 return debugfs_create_file(name, mode, parent, value, &fops_size_t);
393 }
394 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
395
396 static int debugfs_atomic_t_set(void *data, u64 val)
397 {
398 atomic_set((atomic_t *)data, val);
399 return 0;
400 }
401 static int debugfs_atomic_t_get(void *data, u64 *val)
402 {
403 *val = atomic_read((atomic_t *)data);
404 return 0;
405 }
406 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
407 debugfs_atomic_t_set, "%lld\n");
408 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
409 DEFINE_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 */
422 struct 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 }
436 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
437
438 static ssize_t read_file_bool(struct file *file, char __user *user_buf,
439 size_t count, loff_t *ppos)
440 {
441 char buf[3];
442 u32 *val = file->private_data;
443
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 }
452
453 static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
454 size_t count, loff_t *ppos)
455 {
456 char buf[32];
457 size_t buf_size;
458 bool bv;
459 u32 *val = file->private_data;
460
461 buf_size = min(count, (sizeof(buf)-1));
462 if (copy_from_user(buf, user_buf, buf_size))
463 return -EFAULT;
464
465 buf[buf_size] = '\0';
466 if (strtobool(buf, &bv) == 0)
467 *val = bv;
468
469 return count;
470 }
471
472 static const struct file_operations fops_bool = {
473 .read = read_file_bool,
474 .write = write_file_bool,
475 .open = simple_open,
476 .llseek = default_llseek,
477 };
478
479 /**
480 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
481 * @name: a pointer to a string containing the name of the file to create.
482 * @mode: the permission that the file should have
483 * @parent: a pointer to the parent dentry for this file. This should be a
484 * directory dentry if set. If this parameter is %NULL, then the
485 * file will be created in the root of the debugfs filesystem.
486 * @value: a pointer to the variable that the file should read to and write
487 * from.
488 *
489 * This function creates a file in debugfs with the given name that
490 * contains the value of the variable @value. If the @mode variable is so
491 * set, it can be read from, and written to.
492 *
493 * This function will return a pointer to a dentry if it succeeds. This
494 * pointer must be passed to the debugfs_remove() function when the file is
495 * to be removed (no automatic cleanup happens if your module is unloaded,
496 * you are responsible here.) If an error occurs, %NULL will be returned.
497 *
498 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
499 * returned. It is not wise to check for this value, but rather, check for
500 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
501 * code.
502 */
503 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
504 struct dentry *parent, u32 *value)
505 {
506 return debugfs_create_file(name, mode, parent, value, &fops_bool);
507 }
508 EXPORT_SYMBOL_GPL(debugfs_create_bool);
509
510 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
511 size_t count, loff_t *ppos)
512 {
513 struct debugfs_blob_wrapper *blob = file->private_data;
514 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
515 blob->size);
516 }
517
518 static const struct file_operations fops_blob = {
519 .read = read_file_blob,
520 .open = simple_open,
521 .llseek = default_llseek,
522 };
523
524 /**
525 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
526 * @name: a pointer to a string containing the name of the file to create.
527 * @mode: the permission that the file should have
528 * @parent: a pointer to the parent dentry for this file. This should be a
529 * directory dentry if set. If this parameter is %NULL, then the
530 * file will be created in the root of the debugfs filesystem.
531 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
532 * to the blob data and the size of the data.
533 *
534 * This function creates a file in debugfs with the given name that exports
535 * @blob->data as a binary blob. If the @mode variable is so set it can be
536 * read from. Writing is not supported.
537 *
538 * This function will return a pointer to a dentry if it succeeds. This
539 * pointer must be passed to the debugfs_remove() function when the file is
540 * to be removed (no automatic cleanup happens if your module is unloaded,
541 * you are responsible here.) If an error occurs, %NULL will be returned.
542 *
543 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
544 * returned. It is not wise to check for this value, but rather, check for
545 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
546 * code.
547 */
548 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
549 struct dentry *parent,
550 struct debugfs_blob_wrapper *blob)
551 {
552 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
553 }
554 EXPORT_SYMBOL_GPL(debugfs_create_blob);
555
556 struct array_data {
557 void *array;
558 u32 elements;
559 };
560
561 static size_t u32_format_array(char *buf, size_t bufsize,
562 u32 *array, int array_size)
563 {
564 size_t ret = 0;
565
566 while (--array_size >= 0) {
567 size_t len;
568 char term = array_size ? ' ' : '\n';
569
570 len = snprintf(buf, bufsize, "%u%c", *array++, term);
571 ret += len;
572
573 buf += len;
574 bufsize -= len;
575 }
576 return ret;
577 }
578
579 static int u32_array_open(struct inode *inode, struct file *file)
580 {
581 struct array_data *data = inode->i_private;
582 int size, elements = data->elements;
583 char *buf;
584
585 /*
586 * Max size:
587 * - 10 digits + ' '/'\n' = 11 bytes per number
588 * - terminating NUL character
589 */
590 size = elements*11;
591 buf = kmalloc(size+1, GFP_KERNEL);
592 if (!buf)
593 return -ENOMEM;
594 buf[size] = 0;
595
596 file->private_data = buf;
597 u32_format_array(buf, size, data->array, data->elements);
598
599 return nonseekable_open(inode, file);
600 }
601
602 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
603 loff_t *ppos)
604 {
605 size_t size = strlen(file->private_data);
606
607 return simple_read_from_buffer(buf, len, ppos,
608 file->private_data, size);
609 }
610
611 static int u32_array_release(struct inode *inode, struct file *file)
612 {
613 kfree(file->private_data);
614
615 return 0;
616 }
617
618 static const struct file_operations u32_array_fops = {
619 .owner = THIS_MODULE,
620 .open = u32_array_open,
621 .release = u32_array_release,
622 .read = u32_array_read,
623 .llseek = no_llseek,
624 };
625
626 /**
627 * debugfs_create_u32_array - create a debugfs file that is used to read u32
628 * array.
629 * @name: a pointer to a string containing the name of the file to create.
630 * @mode: the permission that the file should have.
631 * @parent: a pointer to the parent dentry for this file. This should be a
632 * directory dentry if set. If this parameter is %NULL, then the
633 * file will be created in the root of the debugfs filesystem.
634 * @array: u32 array that provides data.
635 * @elements: total number of elements in the array.
636 *
637 * This function creates a file in debugfs with the given name that exports
638 * @array as data. If the @mode variable is so set it can be read from.
639 * Writing is not supported. Seek within the file is also not supported.
640 * Once array is created its size can not be changed.
641 *
642 * The function returns a pointer to dentry on success. If debugfs is not
643 * enabled in the kernel, the value -%ENODEV will be returned.
644 */
645 struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
646 struct dentry *parent,
647 u32 *array, u32 elements)
648 {
649 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
650
651 if (data == NULL)
652 return NULL;
653
654 data->array = array;
655 data->elements = elements;
656
657 return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
658 }
659 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
660
661 #ifdef CONFIG_HAS_IOMEM
662
663 /*
664 * The regset32 stuff is used to print 32-bit registers using the
665 * seq_file utilities. We offer printing a register set in an already-opened
666 * sequential file or create a debugfs file that only prints a regset32.
667 */
668
669 /**
670 * debugfs_print_regs32 - use seq_print to describe a set of registers
671 * @s: the seq_file structure being used to generate output
672 * @regs: an array if struct debugfs_reg32 structures
673 * @nregs: the length of the above array
674 * @base: the base address to be used in reading the registers
675 * @prefix: a string to be prefixed to every output line
676 *
677 * This function outputs a text block describing the current values of
678 * some 32-bit hardware registers. It is meant to be used within debugfs
679 * files based on seq_file that need to show registers, intermixed with other
680 * information. The prefix argument may be used to specify a leading string,
681 * because some peripherals have several blocks of identical registers,
682 * for example configuration of dma channels
683 */
684 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
685 int nregs, void __iomem *base, char *prefix)
686 {
687 int i;
688
689 for (i = 0; i < nregs; i++, regs++) {
690 if (prefix)
691 seq_printf(s, "%s", prefix);
692 seq_printf(s, "%s = 0x%08x\n", regs->name,
693 readl(base + regs->offset));
694 if (seq_has_overflowed(s))
695 break;
696 }
697 }
698 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
699
700 static int debugfs_show_regset32(struct seq_file *s, void *data)
701 {
702 struct debugfs_regset32 *regset = s->private;
703
704 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
705 return 0;
706 }
707
708 static int debugfs_open_regset32(struct inode *inode, struct file *file)
709 {
710 return single_open(file, debugfs_show_regset32, inode->i_private);
711 }
712
713 static const struct file_operations fops_regset32 = {
714 .open = debugfs_open_regset32,
715 .read = seq_read,
716 .llseek = seq_lseek,
717 .release = single_release,
718 };
719
720 /**
721 * debugfs_create_regset32 - create a debugfs file that returns register values
722 * @name: a pointer to a string containing the name of the file to create.
723 * @mode: the permission that the file should have
724 * @parent: a pointer to the parent dentry for this file. This should be a
725 * directory dentry if set. If this parameter is %NULL, then the
726 * file will be created in the root of the debugfs filesystem.
727 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
728 * to an array of register definitions, the array size and the base
729 * address where the register bank is to be found.
730 *
731 * This function creates a file in debugfs with the given name that reports
732 * the names and values of a set of 32-bit registers. If the @mode variable
733 * is so set it can be read from. Writing is not supported.
734 *
735 * This function will return a pointer to a dentry if it succeeds. This
736 * pointer must be passed to the debugfs_remove() function when the file is
737 * to be removed (no automatic cleanup happens if your module is unloaded,
738 * you are responsible here.) If an error occurs, %NULL will be returned.
739 *
740 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
741 * returned. It is not wise to check for this value, but rather, check for
742 * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
743 * code.
744 */
745 struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
746 struct dentry *parent,
747 struct debugfs_regset32 *regset)
748 {
749 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
750 }
751 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
752
753 #endif /* CONFIG_HAS_IOMEM */
754
755 struct debugfs_devm_entry {
756 int (*read)(struct seq_file *seq, void *data);
757 struct device *dev;
758 };
759
760 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
761 {
762 struct debugfs_devm_entry *entry = inode->i_private;
763
764 return single_open(f, entry->read, entry->dev);
765 }
766
767 static const struct file_operations debugfs_devm_entry_ops = {
768 .owner = THIS_MODULE,
769 .open = debugfs_devm_entry_open,
770 .release = single_release,
771 .read = seq_read,
772 .llseek = seq_lseek
773 };
774
775 /**
776 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
777 *
778 * @dev: device related to this debugfs file.
779 * @name: name of the debugfs file.
780 * @parent: a pointer to the parent dentry for this file. This should be a
781 * directory dentry if set. If this parameter is %NULL, then the
782 * file will be created in the root of the debugfs filesystem.
783 * @read_fn: function pointer called to print the seq_file content.
784 */
785 struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
786 struct dentry *parent,
787 int (*read_fn)(struct seq_file *s,
788 void *data))
789 {
790 struct debugfs_devm_entry *entry;
791
792 if (IS_ERR(parent))
793 return ERR_PTR(-ENOENT);
794
795 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
796 if (!entry)
797 return ERR_PTR(-ENOMEM);
798
799 entry->read = read_fn;
800 entry->dev = dev;
801
802 return debugfs_create_file(name, S_IRUGO, parent, entry,
803 &debugfs_devm_entry_ops);
804 }
805 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
806
This page took 0.04992 seconds and 5 git commands to generate.