1
0

chardev2.c 6.6 KB

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