chardev2.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * chardev2.c - Create an input/output character device
  3. */
  4. #include <linux/atomic.h>
  5. #include <linux/cdev.h>
  6. #include <linux/delay.h>
  7. #include <linux/device.h>
  8. #include <linux/fs.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h> /* Specifically, a module */
  11. #include <linux/printk.h>
  12. #include <linux/types.h>
  13. #include <linux/uaccess.h> /* for get_user and put_user */
  14. #include <linux/version.h>
  15. #include <asm/errno.h>
  16. #include "chardev.h"
  17. #define SUCCESS 0
  18. #define DEVICE_NAME "char_dev"
  19. #define BUF_LEN 80
  20. enum {
  21. CDEV_NOT_USED = 0,
  22. CDEV_EXCLUSIVE_OPEN = 1,
  23. };
  24. /* Is the device open right now? Used to prevent concurrent access into
  25. * the same device
  26. */
  27. static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED);
  28. /* The message the device will give when asked */
  29. static char message[BUF_LEN + 1];
  30. static struct class *cls;
  31. /* This is called whenever a process attempts to open the device file */
  32. static int device_open(struct inode *inode, struct file *file)
  33. {
  34. pr_info("device_open(%p)\n", file);
  35. try_module_get(THIS_MODULE);
  36. return SUCCESS;
  37. }
  38. static int device_release(struct inode *inode, struct file *file)
  39. {
  40. pr_info("device_release(%p,%p)\n", inode, file);
  41. module_put(THIS_MODULE);
  42. return SUCCESS;
  43. }
  44. /* This function is called whenever a process which has already opened the
  45. * device file attempts to read from it.
  46. */
  47. static ssize_t device_read(struct file *file, /* see include/linux/fs.h */
  48. char __user *buffer, /* buffer to be filled */
  49. size_t length, /* length of the buffer */
  50. loff_t *offset)
  51. {
  52. /* Number of bytes actually written to the buffer */
  53. int bytes_read = 0;
  54. /* How far did the process reading the message get? Useful if the message
  55. * is larger than the size of the buffer we get to fill in device_read.
  56. */
  57. const char *message_ptr = message;
  58. if (!*(message_ptr + *offset)) { /* we are at the end of message */
  59. *offset = 0; /* reset the offset */
  60. return 0; /* signify end of file */
  61. }
  62. message_ptr += *offset;
  63. /* Actually put the data into the buffer */
  64. while (length && *message_ptr) {
  65. /* Because the buffer is in the user data segment, not the kernel
  66. * data segment, assignment would not work. Instead, we have to
  67. * use put_user which copies data from the kernel data segment to
  68. * the user data segment.
  69. */
  70. put_user(*(message_ptr++), buffer++);
  71. length--;
  72. bytes_read++;
  73. }
  74. pr_info("Read %d bytes, %ld left\n", bytes_read, length);
  75. *offset += bytes_read;
  76. /* Read functions are supposed to return the number of bytes actually
  77. * inserted into the buffer.
  78. */
  79. return bytes_read;
  80. }
  81. /* called when somebody tries to write into our device file. */
  82. static ssize_t device_write(struct file *file, const char __user *buffer,
  83. size_t length, loff_t *offset)
  84. {
  85. int i;
  86. pr_info("device_write(%p,%p,%ld)", file, buffer, length);
  87. for (i = 0; i < length && i < BUF_LEN; i++)
  88. get_user(message[i], buffer + i);
  89. /* Again, return the number of input characters used. */
  90. return i;
  91. }
  92. /* This function is called whenever a process tries to do an ioctl on our
  93. * device file. We get two extra parameters (additional to the inode and file
  94. * structures, which all device functions get): the number of the ioctl called
  95. * and the parameter given to the ioctl function.
  96. *
  97. * If the ioctl is write or read/write (meaning output is returned to the
  98. * calling process), the ioctl call returns the output of this function.
  99. */
  100. static long
  101. device_ioctl(struct file *file, /* ditto */
  102. unsigned int ioctl_num, /* number and param for ioctl */
  103. unsigned long ioctl_param)
  104. {
  105. int i;
  106. long ret = SUCCESS;
  107. /* We don't want to talk to two processes at the same time. */
  108. if (atomic_cmpxchg(&already_open, CDEV_NOT_USED, CDEV_EXCLUSIVE_OPEN))
  109. return -EBUSY;
  110. /* Switch according to the ioctl called */
  111. switch (ioctl_num) {
  112. case IOCTL_SET_MSG: {
  113. /* Receive a pointer to a message (in user space) and set that to
  114. * be the device's message. Get the parameter given to ioctl by
  115. * the process.
  116. */
  117. char __user *tmp = (char __user *)ioctl_param;
  118. char ch;
  119. /* Find the length of the message */
  120. get_user(ch, tmp);
  121. for (i = 0; ch && i < BUF_LEN; i++, tmp++)
  122. get_user(ch, tmp);
  123. device_write(file, (char __user *)ioctl_param, i, NULL);
  124. break;
  125. }
  126. case IOCTL_GET_MSG: {
  127. loff_t offset = 0;
  128. /* Give the current message to the calling process - the parameter
  129. * we got is a pointer, fill it.
  130. */
  131. i = device_read(file, (char __user *)ioctl_param, 99, &offset);
  132. /* Put a zero at the end of the buffer, so it will be properly
  133. * terminated.
  134. */
  135. put_user('\0', (char __user *)ioctl_param + i);
  136. break;
  137. }
  138. case IOCTL_GET_NTH_BYTE:
  139. /* This ioctl is both input (ioctl_param) and output (the return
  140. * value of this function).
  141. */
  142. ret = (long)message[ioctl_param];
  143. break;
  144. }
  145. /* We're now ready for our next caller */
  146. atomic_set(&already_open, CDEV_NOT_USED);
  147. return ret;
  148. }
  149. /* Module Declarations */
  150. /* This structure will hold the functions to be called when a process does
  151. * something to the device we created. Since a pointer to this structure
  152. * is kept in the devices table, it can't be local to init_module. NULL is
  153. * for unimplemented functions.
  154. */
  155. static struct file_operations fops = {
  156. .read = device_read,
  157. .write = device_write,
  158. .unlocked_ioctl = device_ioctl,
  159. .open = device_open,
  160. .release = device_release, /* a.k.a. close */
  161. };
  162. /* Initialize the module - Register the character device */
  163. static int __init chardev2_init(void)
  164. {
  165. /* Register the character device (atleast try) */
  166. int ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &fops);
  167. /* Negative values signify an error */
  168. if (ret_val < 0) {
  169. pr_alert("%s failed with %d\n",
  170. "Sorry, registering the character device ", ret_val);
  171. return ret_val;
  172. }
  173. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
  174. cls = class_create(DEVICE_FILE_NAME);
  175. #else
  176. cls = class_create(THIS_MODULE, DEVICE_FILE_NAME);
  177. #endif
  178. device_create(cls, NULL, MKDEV(MAJOR_NUM, 0), NULL, DEVICE_FILE_NAME);
  179. pr_info("Device created on /dev/%s\n", DEVICE_FILE_NAME);
  180. return 0;
  181. }
  182. /* Cleanup - unregister the appropriate file from /proc */
  183. static void __exit chardev2_exit(void)
  184. {
  185. device_destroy(cls, MKDEV(MAJOR_NUM, 0));
  186. class_destroy(cls);
  187. /* Unregister the device */
  188. unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
  189. }
  190. module_init(chardev2_init);
  191. module_exit(chardev2_exit);
  192. MODULE_LICENSE("GPL");