chardev2.c 7.0 KB

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