chardev2.c 6.5 KB

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