character_device.c 850 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* https://github.com/cirosantilli/linux-kernel-module-cheat#character-devices */
  2. #include <linux/fs.h> /* register_chrdev, unregister_chrdev */
  3. #include <linux/module.h>
  4. #include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
  5. #define NAME "lkmc_character_device"
  6. static int major;
  7. static int show(struct seq_file *m, void *v)
  8. {
  9. seq_printf(m, "abcd");
  10. return 0;
  11. }
  12. static int open(struct inode *inode, struct file *file)
  13. {
  14. return single_open(file, show, NULL);
  15. }
  16. static const struct file_operations fops = {
  17. .llseek = seq_lseek,
  18. .open = open,
  19. .owner = THIS_MODULE,
  20. .read = seq_read,
  21. .release = single_release,
  22. };
  23. static int myinit(void)
  24. {
  25. major = register_chrdev(0, NAME, &fops);
  26. return 0;
  27. }
  28. static void myexit(void)
  29. {
  30. unregister_chrdev(major, NAME);
  31. }
  32. module_init(myinit)
  33. module_exit(myexit)
  34. MODULE_LICENSE("GPL");