1
0

chardev.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * chardev.c: Creates a read-only char device that says how many times
  3. * you have read from the dev file
  4. */
  5. #include <linux/atomic.h>
  6. #include <linux/cdev.h>
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/fs.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h> /* for sprintf() */
  12. #include <linux/module.h>
  13. #include <linux/printk.h>
  14. #include <linux/types.h>
  15. #include <linux/uaccess.h> /* for get_user and put_user */
  16. #include <linux/version.h>
  17. #include <asm/errno.h>
  18. /* Prototypes - this would normally go in a .h file */
  19. static int device_open(struct inode *, struct file *);
  20. static int device_release(struct inode *, struct file *);
  21. static ssize_t device_read(struct file *, char __user *, size_t, loff_t *);
  22. static ssize_t device_write(struct file *, const char __user *, size_t,
  23. loff_t *);
  24. #define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
  25. #define BUF_LEN 80 /* Max length of the message from the device */
  26. /* Global variables are declared as static, so are global within the file. */
  27. static int major; /* major number assigned to our device driver */
  28. enum {
  29. CDEV_NOT_USED,
  30. CDEV_EXCLUSIVE_OPEN,
  31. };
  32. /* Is device open? Used to prevent multiple access to device */
  33. static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED);
  34. static char msg[BUF_LEN + 1]; /* The msg the device will give when asked */
  35. static struct class *cls;
  36. static struct file_operations chardev_fops = {
  37. .read = device_read,
  38. .write = device_write,
  39. .open = device_open,
  40. .release = device_release,
  41. };
  42. static int __init chardev_init(void)
  43. {
  44. major = register_chrdev(0, DEVICE_NAME, &chardev_fops);
  45. if (major < 0) {
  46. pr_alert("Registering char device failed with %d\n", major);
  47. return major;
  48. }
  49. pr_info("I was assigned major number %d.\n", major);
  50. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
  51. cls = class_create(DEVICE_NAME);
  52. #else
  53. cls = class_create(THIS_MODULE, DEVICE_NAME);
  54. #endif
  55. device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
  56. pr_info("Device created on /dev/%s\n", DEVICE_NAME);
  57. return 0;
  58. }
  59. static void __exit chardev_exit(void)
  60. {
  61. device_destroy(cls, MKDEV(major, 0));
  62. class_destroy(cls);
  63. /* Unregister the device */
  64. unregister_chrdev(major, DEVICE_NAME);
  65. }
  66. /* Methods */
  67. /* Called when a process tries to open the device file, like
  68. * "sudo cat /dev/chardev"
  69. */
  70. static int device_open(struct inode *inode, struct file *file)
  71. {
  72. static int counter = 0;
  73. if (atomic_cmpxchg(&already_open, CDEV_NOT_USED, CDEV_EXCLUSIVE_OPEN))
  74. return -EBUSY;
  75. sprintf(msg, "I already told you %d times Hello world!\n", counter++);
  76. try_module_get(THIS_MODULE);
  77. return 0;
  78. }
  79. /* Called when a process closes the device file. */
  80. static int device_release(struct inode *inode, struct file *file)
  81. {
  82. /* We're now ready for our next caller */
  83. atomic_set(&already_open, CDEV_NOT_USED);
  84. /* Decrement the usage count, or else once you opened the file, you will
  85. * never get rid of the module.
  86. */
  87. module_put(THIS_MODULE);
  88. return 0;
  89. }
  90. /* Called when a process, which already opened the dev file, attempts to
  91. * read from it.
  92. */
  93. static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
  94. char __user *buffer, /* buffer to fill with data */
  95. size_t length, /* length of the buffer */
  96. loff_t *offset)
  97. {
  98. /* Number of bytes actually written to the buffer */
  99. int bytes_read = 0;
  100. const char *msg_ptr = msg;
  101. if (!*(msg_ptr + *offset)) { /* we are at the end of message */
  102. *offset = 0; /* reset the offset */
  103. return 0; /* signify end of file */
  104. }
  105. msg_ptr += *offset;
  106. /* Actually put the data into the buffer */
  107. while (length && *msg_ptr) {
  108. /* The buffer is in the user data segment, not the kernel
  109. * segment so "*" assignment won't work. We have to use
  110. * put_user which copies data from the kernel data segment to
  111. * the user data segment.
  112. */
  113. put_user(*(msg_ptr++), buffer++);
  114. length--;
  115. bytes_read++;
  116. }
  117. *offset += bytes_read;
  118. /* Most read functions return the number of bytes put into the buffer. */
  119. return bytes_read;
  120. }
  121. /* Called when a process writes to dev file: echo "hi" > /dev/hello */
  122. static ssize_t device_write(struct file *filp, const char __user *buff,
  123. size_t len, loff_t *off)
  124. {
  125. pr_alert("Sorry, this operation is not supported.\n");
  126. return -EINVAL;
  127. }
  128. module_init(chardev_init);
  129. module_exit(chardev_exit);
  130. MODULE_LICENSE("GPL");