1
0

chardev2.c 6.9 KB

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