devices.c 1.4 KB

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