chardev2.c 6.8 KB

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