1
0

chardev.c 4.7 KB

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