This source file includes following definitions.
- default_read_file
- default_write_file
- debugfs_real_fops
- debugfs_file_get
- debugfs_file_put
- debugfs_locked_down
- open_proxy_open
- full_proxy_poll
- full_proxy_release
- __full_proxy_fops_init
- full_proxy_open
- debugfs_attr_read
- debugfs_attr_write
- debugfs_create_mode_unsafe
- debugfs_u8_set
- debugfs_u8_get
- debugfs_create_u8
- debugfs_u16_set
- debugfs_u16_get
- debugfs_create_u16
- debugfs_u32_set
- debugfs_u32_get
- debugfs_create_u32
- debugfs_u64_set
- debugfs_u64_get
- debugfs_create_u64
- debugfs_ulong_set
- debugfs_ulong_get
- debugfs_create_ulong
- debugfs_create_x8
- debugfs_create_x16
- debugfs_create_x32
- debugfs_create_x64
- debugfs_size_t_set
- debugfs_size_t_get
- debugfs_create_size_t
- debugfs_atomic_t_set
- debugfs_atomic_t_get
- debugfs_create_atomic_t
- debugfs_read_file_bool
- debugfs_write_file_bool
- debugfs_create_bool
- read_file_blob
- debugfs_create_blob
- u32_format_array
- u32_array_open
- u32_array_read
- u32_array_release
- debugfs_create_u32_array
- debugfs_print_regs32
- debugfs_show_regset32
- debugfs_open_regset32
- debugfs_create_regset32
- debugfs_devm_entry_open
- debugfs_create_devm_seqfile
1
2
3
4
5
6
7
8
9
10
11
12 #include <linux/module.h>
13 #include <linux/fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/pagemap.h>
16 #include <linux/debugfs.h>
17 #include <linux/io.h>
18 #include <linux/slab.h>
19 #include <linux/atomic.h>
20 #include <linux/device.h>
21 #include <linux/poll.h>
22 #include <linux/security.h>
23
24 #include "internal.h"
25
26 struct poll_table_struct;
27
28 static ssize_t default_read_file(struct file *file, char __user *buf,
29 size_t count, loff_t *ppos)
30 {
31 return 0;
32 }
33
34 static ssize_t default_write_file(struct file *file, const char __user *buf,
35 size_t count, loff_t *ppos)
36 {
37 return count;
38 }
39
40 const struct file_operations debugfs_noop_file_operations = {
41 .read = default_read_file,
42 .write = default_write_file,
43 .open = simple_open,
44 .llseek = noop_llseek,
45 };
46
47 #define F_DENTRY(filp) ((filp)->f_path.dentry)
48
49 const struct file_operations *debugfs_real_fops(const struct file *filp)
50 {
51 struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
52
53 if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
54
55
56
57
58 WARN_ON(1);
59 return NULL;
60 }
61
62 return fsd->real_fops;
63 }
64 EXPORT_SYMBOL_GPL(debugfs_real_fops);
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 int debugfs_file_get(struct dentry *dentry)
82 {
83 struct debugfs_fsdata *fsd;
84 void *d_fsd;
85
86 d_fsd = READ_ONCE(dentry->d_fsdata);
87 if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
88 fsd = d_fsd;
89 } else {
90 fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
91 if (!fsd)
92 return -ENOMEM;
93
94 fsd->real_fops = (void *)((unsigned long)d_fsd &
95 ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
96 refcount_set(&fsd->active_users, 1);
97 init_completion(&fsd->active_users_drained);
98 if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
99 kfree(fsd);
100 fsd = READ_ONCE(dentry->d_fsdata);
101 }
102 }
103
104
105
106
107
108
109
110
111
112 if (d_unlinked(dentry))
113 return -EIO;
114
115 if (!refcount_inc_not_zero(&fsd->active_users))
116 return -EIO;
117
118 return 0;
119 }
120 EXPORT_SYMBOL_GPL(debugfs_file_get);
121
122
123
124
125
126
127
128
129
130
131 void debugfs_file_put(struct dentry *dentry)
132 {
133 struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
134
135 if (refcount_dec_and_test(&fsd->active_users))
136 complete(&fsd->active_users_drained);
137 }
138 EXPORT_SYMBOL_GPL(debugfs_file_put);
139
140
141
142
143
144
145 static int debugfs_locked_down(struct inode *inode,
146 struct file *filp,
147 const struct file_operations *real_fops)
148 {
149 if ((inode->i_mode & 07777) == 0444 &&
150 !(filp->f_mode & FMODE_WRITE) &&
151 !real_fops->unlocked_ioctl &&
152 !real_fops->compat_ioctl &&
153 !real_fops->mmap)
154 return 0;
155
156 if (security_locked_down(LOCKDOWN_DEBUGFS))
157 return -EPERM;
158
159 return 0;
160 }
161
162 static int open_proxy_open(struct inode *inode, struct file *filp)
163 {
164 struct dentry *dentry = F_DENTRY(filp);
165 const struct file_operations *real_fops = NULL;
166 int r;
167
168 r = debugfs_file_get(dentry);
169 if (r)
170 return r == -EIO ? -ENOENT : r;
171
172 real_fops = debugfs_real_fops(filp);
173
174 r = debugfs_locked_down(inode, filp, real_fops);
175 if (r)
176 goto out;
177
178 if (!fops_get(real_fops)) {
179 #ifdef MODULE
180 if (real_fops->owner &&
181 real_fops->owner->state == MODULE_STATE_GOING)
182 goto out;
183 #endif
184
185
186 WARN(1, "debugfs file owner did not clean up at exit: %pd",
187 dentry);
188 r = -ENXIO;
189 goto out;
190 }
191 replace_fops(filp, real_fops);
192
193 if (real_fops->open)
194 r = real_fops->open(inode, filp);
195
196 out:
197 debugfs_file_put(dentry);
198 return r;
199 }
200
201 const struct file_operations debugfs_open_proxy_file_operations = {
202 .open = open_proxy_open,
203 };
204
205 #define PROTO(args...) args
206 #define ARGS(args...) args
207
208 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
209 static ret_type full_proxy_ ## name(proto) \
210 { \
211 struct dentry *dentry = F_DENTRY(filp); \
212 const struct file_operations *real_fops; \
213 ret_type r; \
214 \
215 r = debugfs_file_get(dentry); \
216 if (unlikely(r)) \
217 return r; \
218 real_fops = debugfs_real_fops(filp); \
219 r = real_fops->name(args); \
220 debugfs_file_put(dentry); \
221 return r; \
222 }
223
224 FULL_PROXY_FUNC(llseek, loff_t, filp,
225 PROTO(struct file *filp, loff_t offset, int whence),
226 ARGS(filp, offset, whence));
227
228 FULL_PROXY_FUNC(read, ssize_t, filp,
229 PROTO(struct file *filp, char __user *buf, size_t size,
230 loff_t *ppos),
231 ARGS(filp, buf, size, ppos));
232
233 FULL_PROXY_FUNC(write, ssize_t, filp,
234 PROTO(struct file *filp, const char __user *buf, size_t size,
235 loff_t *ppos),
236 ARGS(filp, buf, size, ppos));
237
238 FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
239 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
240 ARGS(filp, cmd, arg));
241
242 static __poll_t full_proxy_poll(struct file *filp,
243 struct poll_table_struct *wait)
244 {
245 struct dentry *dentry = F_DENTRY(filp);
246 __poll_t r = 0;
247 const struct file_operations *real_fops;
248
249 if (debugfs_file_get(dentry))
250 return EPOLLHUP;
251
252 real_fops = debugfs_real_fops(filp);
253 r = real_fops->poll(filp, wait);
254 debugfs_file_put(dentry);
255 return r;
256 }
257
258 static int full_proxy_release(struct inode *inode, struct file *filp)
259 {
260 const struct dentry *dentry = F_DENTRY(filp);
261 const struct file_operations *real_fops = debugfs_real_fops(filp);
262 const struct file_operations *proxy_fops = filp->f_op;
263 int r = 0;
264
265
266
267
268
269
270
271 if (real_fops->release)
272 r = real_fops->release(inode, filp);
273
274 replace_fops(filp, d_inode(dentry)->i_fop);
275 kfree((void *)proxy_fops);
276 fops_put(real_fops);
277 return r;
278 }
279
280 static void __full_proxy_fops_init(struct file_operations *proxy_fops,
281 const struct file_operations *real_fops)
282 {
283 proxy_fops->release = full_proxy_release;
284 if (real_fops->llseek)
285 proxy_fops->llseek = full_proxy_llseek;
286 if (real_fops->read)
287 proxy_fops->read = full_proxy_read;
288 if (real_fops->write)
289 proxy_fops->write = full_proxy_write;
290 if (real_fops->poll)
291 proxy_fops->poll = full_proxy_poll;
292 if (real_fops->unlocked_ioctl)
293 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
294 }
295
296 static int full_proxy_open(struct inode *inode, struct file *filp)
297 {
298 struct dentry *dentry = F_DENTRY(filp);
299 const struct file_operations *real_fops = NULL;
300 struct file_operations *proxy_fops = NULL;
301 int r;
302
303 r = debugfs_file_get(dentry);
304 if (r)
305 return r == -EIO ? -ENOENT : r;
306
307 real_fops = debugfs_real_fops(filp);
308
309 r = debugfs_locked_down(inode, filp, real_fops);
310 if (r)
311 goto out;
312
313 if (!fops_get(real_fops)) {
314 #ifdef MODULE
315 if (real_fops->owner &&
316 real_fops->owner->state == MODULE_STATE_GOING)
317 goto out;
318 #endif
319
320
321 WARN(1, "debugfs file owner did not clean up at exit: %pd",
322 dentry);
323 r = -ENXIO;
324 goto out;
325 }
326
327 proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
328 if (!proxy_fops) {
329 r = -ENOMEM;
330 goto free_proxy;
331 }
332 __full_proxy_fops_init(proxy_fops, real_fops);
333 replace_fops(filp, proxy_fops);
334
335 if (real_fops->open) {
336 r = real_fops->open(inode, filp);
337 if (r) {
338 replace_fops(filp, d_inode(dentry)->i_fop);
339 goto free_proxy;
340 } else if (filp->f_op != proxy_fops) {
341
342 WARN(1, "debugfs file owner replaced proxy fops: %pd",
343 dentry);
344 goto free_proxy;
345 }
346 }
347
348 goto out;
349 free_proxy:
350 kfree(proxy_fops);
351 fops_put(real_fops);
352 out:
353 debugfs_file_put(dentry);
354 return r;
355 }
356
357 const struct file_operations debugfs_full_proxy_file_operations = {
358 .open = full_proxy_open,
359 };
360
361 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
362 size_t len, loff_t *ppos)
363 {
364 struct dentry *dentry = F_DENTRY(file);
365 ssize_t ret;
366
367 ret = debugfs_file_get(dentry);
368 if (unlikely(ret))
369 return ret;
370 ret = simple_attr_read(file, buf, len, ppos);
371 debugfs_file_put(dentry);
372 return ret;
373 }
374 EXPORT_SYMBOL_GPL(debugfs_attr_read);
375
376 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
377 size_t len, loff_t *ppos)
378 {
379 struct dentry *dentry = F_DENTRY(file);
380 ssize_t ret;
381
382 ret = debugfs_file_get(dentry);
383 if (unlikely(ret))
384 return ret;
385 ret = simple_attr_write(file, buf, len, ppos);
386 debugfs_file_put(dentry);
387 return ret;
388 }
389 EXPORT_SYMBOL_GPL(debugfs_attr_write);
390
391 static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
392 struct dentry *parent, void *value,
393 const struct file_operations *fops,
394 const struct file_operations *fops_ro,
395 const struct file_operations *fops_wo)
396 {
397
398 if (!(mode & S_IWUGO))
399 return debugfs_create_file_unsafe(name, mode, parent, value,
400 fops_ro);
401
402 if (!(mode & S_IRUGO))
403 return debugfs_create_file_unsafe(name, mode, parent, value,
404 fops_wo);
405
406 return debugfs_create_file_unsafe(name, mode, parent, value, fops);
407 }
408
409 static int debugfs_u8_set(void *data, u64 val)
410 {
411 *(u8 *)data = val;
412 return 0;
413 }
414 static int debugfs_u8_get(void *data, u64 *val)
415 {
416 *val = *(u8 *)data;
417 return 0;
418 }
419 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
420 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
421 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446 struct dentry *debugfs_create_u8(const char *name, umode_t mode,
447 struct dentry *parent, u8 *value)
448 {
449 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
450 &fops_u8_ro, &fops_u8_wo);
451 }
452 EXPORT_SYMBOL_GPL(debugfs_create_u8);
453
454 static int debugfs_u16_set(void *data, u64 val)
455 {
456 *(u16 *)data = val;
457 return 0;
458 }
459 static int debugfs_u16_get(void *data, u64 *val)
460 {
461 *val = *(u16 *)data;
462 return 0;
463 }
464 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
465 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
466 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491 struct dentry *debugfs_create_u16(const char *name, umode_t mode,
492 struct dentry *parent, u16 *value)
493 {
494 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
495 &fops_u16_ro, &fops_u16_wo);
496 }
497 EXPORT_SYMBOL_GPL(debugfs_create_u16);
498
499 static int debugfs_u32_set(void *data, u64 val)
500 {
501 *(u32 *)data = val;
502 return 0;
503 }
504 static int debugfs_u32_get(void *data, u64 *val)
505 {
506 *val = *(u32 *)data;
507 return 0;
508 }
509 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
510 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
511 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536 struct dentry *debugfs_create_u32(const char *name, umode_t mode,
537 struct dentry *parent, u32 *value)
538 {
539 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
540 &fops_u32_ro, &fops_u32_wo);
541 }
542 EXPORT_SYMBOL_GPL(debugfs_create_u32);
543
544 static int debugfs_u64_set(void *data, u64 val)
545 {
546 *(u64 *)data = val;
547 return 0;
548 }
549
550 static int debugfs_u64_get(void *data, u64 *val)
551 {
552 *val = *(u64 *)data;
553 return 0;
554 }
555 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
556 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
557 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582 struct dentry *debugfs_create_u64(const char *name, umode_t mode,
583 struct dentry *parent, u64 *value)
584 {
585 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
586 &fops_u64_ro, &fops_u64_wo);
587 }
588 EXPORT_SYMBOL_GPL(debugfs_create_u64);
589
590 static int debugfs_ulong_set(void *data, u64 val)
591 {
592 *(unsigned long *)data = val;
593 return 0;
594 }
595
596 static int debugfs_ulong_get(void *data, u64 *val)
597 {
598 *val = *(unsigned long *)data;
599 return 0;
600 }
601 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
602 "%llu\n");
603 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
604 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630 struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
631 struct dentry *parent, unsigned long *value)
632 {
633 return debugfs_create_mode_unsafe(name, mode, parent, value,
634 &fops_ulong, &fops_ulong_ro,
635 &fops_ulong_wo);
636 }
637 EXPORT_SYMBOL_GPL(debugfs_create_ulong);
638
639 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
640 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
641 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
642
643 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
644 "0x%04llx\n");
645 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
646 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
647
648 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
649 "0x%08llx\n");
650 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
651 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
652
653 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
654 "0x%016llx\n");
655 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
656 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676 struct dentry *debugfs_create_x8(const char *name, umode_t mode,
677 struct dentry *parent, u8 *value)
678 {
679 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
680 &fops_x8_ro, &fops_x8_wo);
681 }
682 EXPORT_SYMBOL_GPL(debugfs_create_x8);
683
684
685
686
687
688
689
690
691
692
693
694 struct dentry *debugfs_create_x16(const char *name, umode_t mode,
695 struct dentry *parent, u16 *value)
696 {
697 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
698 &fops_x16_ro, &fops_x16_wo);
699 }
700 EXPORT_SYMBOL_GPL(debugfs_create_x16);
701
702
703
704
705
706
707
708
709
710
711
712 struct dentry *debugfs_create_x32(const char *name, umode_t mode,
713 struct dentry *parent, u32 *value)
714 {
715 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
716 &fops_x32_ro, &fops_x32_wo);
717 }
718 EXPORT_SYMBOL_GPL(debugfs_create_x32);
719
720
721
722
723
724
725
726
727
728
729
730 struct dentry *debugfs_create_x64(const char *name, umode_t mode,
731 struct dentry *parent, u64 *value)
732 {
733 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
734 &fops_x64_ro, &fops_x64_wo);
735 }
736 EXPORT_SYMBOL_GPL(debugfs_create_x64);
737
738
739 static int debugfs_size_t_set(void *data, u64 val)
740 {
741 *(size_t *)data = val;
742 return 0;
743 }
744 static int debugfs_size_t_get(void *data, u64 *val)
745 {
746 *val = *(size_t *)data;
747 return 0;
748 }
749 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
750 "%llu\n");
751 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
752 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
753
754
755
756
757
758
759
760
761
762
763
764 struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
765 struct dentry *parent, size_t *value)
766 {
767 return debugfs_create_mode_unsafe(name, mode, parent, value,
768 &fops_size_t, &fops_size_t_ro,
769 &fops_size_t_wo);
770 }
771 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
772
773 static int debugfs_atomic_t_set(void *data, u64 val)
774 {
775 atomic_set((atomic_t *)data, val);
776 return 0;
777 }
778 static int debugfs_atomic_t_get(void *data, u64 *val)
779 {
780 *val = atomic_read((atomic_t *)data);
781 return 0;
782 }
783 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
784 debugfs_atomic_t_set, "%lld\n");
785 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
786 "%lld\n");
787 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
788 "%lld\n");
789
790
791
792
793
794
795
796
797
798
799
800
801 struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
802 struct dentry *parent, atomic_t *value)
803 {
804 return debugfs_create_mode_unsafe(name, mode, parent, value,
805 &fops_atomic_t, &fops_atomic_t_ro,
806 &fops_atomic_t_wo);
807 }
808 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
809
810 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
811 size_t count, loff_t *ppos)
812 {
813 char buf[3];
814 bool val;
815 int r;
816 struct dentry *dentry = F_DENTRY(file);
817
818 r = debugfs_file_get(dentry);
819 if (unlikely(r))
820 return r;
821 val = *(bool *)file->private_data;
822 debugfs_file_put(dentry);
823
824 if (val)
825 buf[0] = 'Y';
826 else
827 buf[0] = 'N';
828 buf[1] = '\n';
829 buf[2] = 0x00;
830 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
831 }
832 EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
833
834 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
835 size_t count, loff_t *ppos)
836 {
837 bool bv;
838 int r;
839 bool *val = file->private_data;
840 struct dentry *dentry = F_DENTRY(file);
841
842 r = kstrtobool_from_user(user_buf, count, &bv);
843 if (!r) {
844 r = debugfs_file_get(dentry);
845 if (unlikely(r))
846 return r;
847 *val = bv;
848 debugfs_file_put(dentry);
849 }
850
851 return count;
852 }
853 EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
854
855 static const struct file_operations fops_bool = {
856 .read = debugfs_read_file_bool,
857 .write = debugfs_write_file_bool,
858 .open = simple_open,
859 .llseek = default_llseek,
860 };
861
862 static const struct file_operations fops_bool_ro = {
863 .read = debugfs_read_file_bool,
864 .open = simple_open,
865 .llseek = default_llseek,
866 };
867
868 static const struct file_operations fops_bool_wo = {
869 .write = debugfs_write_file_bool,
870 .open = simple_open,
871 .llseek = default_llseek,
872 };
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
898 struct dentry *parent, bool *value)
899 {
900 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
901 &fops_bool_ro, &fops_bool_wo);
902 }
903 EXPORT_SYMBOL_GPL(debugfs_create_bool);
904
905 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
906 size_t count, loff_t *ppos)
907 {
908 struct debugfs_blob_wrapper *blob = file->private_data;
909 struct dentry *dentry = F_DENTRY(file);
910 ssize_t r;
911
912 r = debugfs_file_get(dentry);
913 if (unlikely(r))
914 return r;
915 r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
916 blob->size);
917 debugfs_file_put(dentry);
918 return r;
919 }
920
921 static const struct file_operations fops_blob = {
922 .read = read_file_blob,
923 .open = simple_open,
924 .llseek = default_llseek,
925 };
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
951 struct dentry *parent,
952 struct debugfs_blob_wrapper *blob)
953 {
954 return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
955 }
956 EXPORT_SYMBOL_GPL(debugfs_create_blob);
957
958 struct array_data {
959 void *array;
960 u32 elements;
961 };
962
963 static size_t u32_format_array(char *buf, size_t bufsize,
964 u32 *array, int array_size)
965 {
966 size_t ret = 0;
967
968 while (--array_size >= 0) {
969 size_t len;
970 char term = array_size ? ' ' : '\n';
971
972 len = snprintf(buf, bufsize, "%u%c", *array++, term);
973 ret += len;
974
975 buf += len;
976 bufsize -= len;
977 }
978 return ret;
979 }
980
981 static int u32_array_open(struct inode *inode, struct file *file)
982 {
983 struct array_data *data = inode->i_private;
984 int size, elements = data->elements;
985 char *buf;
986
987
988
989
990
991
992 size = elements*11;
993 buf = kmalloc(size+1, GFP_KERNEL);
994 if (!buf)
995 return -ENOMEM;
996 buf[size] = 0;
997
998 file->private_data = buf;
999 u32_format_array(buf, size, data->array, data->elements);
1000
1001 return nonseekable_open(inode, file);
1002 }
1003
1004 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
1005 loff_t *ppos)
1006 {
1007 size_t size = strlen(file->private_data);
1008
1009 return simple_read_from_buffer(buf, len, ppos,
1010 file->private_data, size);
1011 }
1012
1013 static int u32_array_release(struct inode *inode, struct file *file)
1014 {
1015 kfree(file->private_data);
1016
1017 return 0;
1018 }
1019
1020 static const struct file_operations u32_array_fops = {
1021 .owner = THIS_MODULE,
1022 .open = u32_array_open,
1023 .release = u32_array_release,
1024 .read = u32_array_read,
1025 .llseek = no_llseek,
1026 };
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044 void debugfs_create_u32_array(const char *name, umode_t mode,
1045 struct dentry *parent, u32 *array, u32 elements)
1046 {
1047 struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
1048
1049 if (data == NULL)
1050 return;
1051
1052 data->array = array;
1053 data->elements = elements;
1054
1055 debugfs_create_file_unsafe(name, mode, parent, data, &u32_array_fops);
1056 }
1057 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1058
1059 #ifdef CONFIG_HAS_IOMEM
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1083 int nregs, void __iomem *base, char *prefix)
1084 {
1085 int i;
1086
1087 for (i = 0; i < nregs; i++, regs++) {
1088 if (prefix)
1089 seq_printf(s, "%s", prefix);
1090 seq_printf(s, "%s = 0x%08x\n", regs->name,
1091 readl(base + regs->offset));
1092 if (seq_has_overflowed(s))
1093 break;
1094 }
1095 }
1096 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1097
1098 static int debugfs_show_regset32(struct seq_file *s, void *data)
1099 {
1100 struct debugfs_regset32 *regset = s->private;
1101
1102 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1103 return 0;
1104 }
1105
1106 static int debugfs_open_regset32(struct inode *inode, struct file *file)
1107 {
1108 return single_open(file, debugfs_show_regset32, inode->i_private);
1109 }
1110
1111 static const struct file_operations fops_regset32 = {
1112 .open = debugfs_open_regset32,
1113 .read = seq_read,
1114 .llseek = seq_lseek,
1115 .release = single_release,
1116 };
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142 struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
1143 struct dentry *parent,
1144 struct debugfs_regset32 *regset)
1145 {
1146 return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
1147 }
1148 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
1149
1150 #endif
1151
1152 struct debugfs_devm_entry {
1153 int (*read)(struct seq_file *seq, void *data);
1154 struct device *dev;
1155 };
1156
1157 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1158 {
1159 struct debugfs_devm_entry *entry = inode->i_private;
1160
1161 return single_open(f, entry->read, entry->dev);
1162 }
1163
1164 static const struct file_operations debugfs_devm_entry_ops = {
1165 .owner = THIS_MODULE,
1166 .open = debugfs_devm_entry_open,
1167 .release = single_release,
1168 .read = seq_read,
1169 .llseek = seq_lseek
1170 };
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182 struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
1183 struct dentry *parent,
1184 int (*read_fn)(struct seq_file *s,
1185 void *data))
1186 {
1187 struct debugfs_devm_entry *entry;
1188
1189 if (IS_ERR(parent))
1190 return ERR_PTR(-ENOENT);
1191
1192 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1193 if (!entry)
1194 return ERR_PTR(-ENOMEM);
1195
1196 entry->read = read_fn;
1197 entry->dev = dev;
1198
1199 return debugfs_create_file(name, S_IRUGO, parent, entry,
1200 &debugfs_devm_entry_ops);
1201 }
1202 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1203