devices.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/fs.h>
  3. #include <linux/init.h>
  4. #include <linux/proc_fs.h>
  5. #include <linux/seq_file.h>
  6. static int devinfo_show(struct seq_file *f, void *v)
  7. {
  8. int i = *(loff_t *) v;
  9. if (i < CHRDEV_MAJOR_MAX) {
  10. if (i == 0)
  11. seq_puts(f, "Character devices:\n");
  12. chrdev_show(f, i);
  13. }
  14. #ifdef CONFIG_BLOCK
  15. else {
  16. i -= CHRDEV_MAJOR_MAX;
  17. if (i == 0)
  18. seq_puts(f, "\nBlock devices:\n");
  19. blkdev_show(f, i);
  20. }
  21. #endif
  22. return 0;
  23. }
  24. static void *devinfo_start(struct seq_file *f, loff_t *pos)
  25. {
  26. if (*pos < (BLKDEV_MAJOR_MAX + CHRDEV_MAJOR_MAX))
  27. return pos;
  28. return NULL;
  29. }
  30. static void *devinfo_next(struct seq_file *f, void *v, loff_t *pos)
  31. {
  32. (*pos)++;
  33. if (*pos >= (BLKDEV_MAJOR_MAX + CHRDEV_MAJOR_MAX))
  34. return NULL;
  35. return pos;
  36. }
  37. static void devinfo_stop(struct seq_file *f, void *v)
  38. {
  39. /* Nothing to do */
  40. }
  41. static const struct seq_operations devinfo_ops = {
  42. .start = devinfo_start,
  43. .next = devinfo_next,
  44. .stop = devinfo_stop,
  45. .show = devinfo_show
  46. };
  47. static int devinfo_open(struct inode *inode, struct file *filp)
  48. {
  49. return seq_open(filp, &devinfo_ops);
  50. }
  51. static const struct file_operations proc_devinfo_operations = {
  52. .open = devinfo_open,
  53. .read = seq_read,
  54. .llseek = seq_lseek,
  55. .release = seq_release,
  56. };
  57. static int __init proc_devices_init(void)
  58. {
  59. proc_create("devices", 0, NULL, &proc_devinfo_operations);
  60. return 0;
  61. }
  62. fs_initcall(proc_devices_init);