params.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-module-parameters */
  2. #include <linux/debugfs.h>
  3. #include <linux/delay.h> /* usleep_range */
  4. #include <linux/kernel.h>
  5. #include <linux/module.h>
  6. #include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
  7. #include <uapi/linux/stat.h> /* S_IRUSR | S_IWUSR */
  8. static u32 i = 0;
  9. static u32 j = 0;
  10. module_param(i, int, S_IRUSR | S_IWUSR);
  11. module_param(j, int, S_IRUSR | S_IWUSR);
  12. MODULE_PARM_DESC(i, "my favorite int");
  13. MODULE_PARM_DESC(j, "my second favorite int");
  14. static struct dentry *debugfs_file;
  15. static int show(struct seq_file *m, void *v)
  16. {
  17. char kbuf[18];
  18. int ret;
  19. ret = snprintf(kbuf, sizeof(kbuf), "%d %d", i, j);
  20. seq_printf(m, kbuf);
  21. return 0;
  22. }
  23. static int open(struct inode *inode, struct file *file)
  24. {
  25. return single_open(file, show, NULL);
  26. }
  27. static const struct file_operations fops = {
  28. .llseek = seq_lseek,
  29. .open = open,
  30. .owner = THIS_MODULE,
  31. .read = seq_read,
  32. .release = single_release,
  33. };
  34. static int myinit(void)
  35. {
  36. debugfs_file = debugfs_create_file("lkmc_params", S_IRUSR, NULL, NULL, &fops);
  37. return 0;
  38. }
  39. static void myexit(void)
  40. {
  41. debugfs_remove(debugfs_file);
  42. }
  43. module_init(myinit)
  44. module_exit(myexit)
  45. MODULE_LICENSE("GPL");