1
0

chardev2.c 6.8 KB

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