chardev2.c 6.5 KB

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