1
0

chardev2.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * chardev2.c - Create an input/output character device
  3. */
  4. #include <asm/io.h>
  5. #include <asm/irq.h>
  6. #include <asm/uaccess.h>
  7. #include <linux/cdev.h>
  8. #include <linux/delay.h>
  9. #include <linux/device.h>
  10. #include <linux/fs.h>
  11. #include <linux/init.h>
  12. #include <linux/irq.h>
  13. #include <linux/kernel.h> /* We're doing kernel work */
  14. #include <linux/module.h> /* Specifically, a module */
  15. #include <linux/poll.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 device_write(struct file *file,
  122. const char __user *buffer,
  123. size_t length,
  124. loff_t *offset)
  125. {
  126. int i;
  127. #ifdef DEBUG
  128. pr_info("device_write(%p,%s,%d)", file, buffer, length);
  129. #endif
  130. for (i = 0; i < length && i < BUF_LEN; i++)
  131. get_user(Message[i], buffer + i);
  132. Message_Ptr = Message;
  133. /*
  134. * Again, return the number of input characters used
  135. */
  136. return i;
  137. }
  138. /*
  139. * This function is called whenever a process tries to do an ioctl on our
  140. * device file. We get two extra parameters (additional to the inode and file
  141. * structures, which all device functions get): the number of the ioctl called
  142. * and the parameter given to the ioctl function.
  143. *
  144. * If the ioctl is write or read/write (meaning output is returned to the
  145. * calling process), the ioctl call returns the output of this function.
  146. *
  147. */
  148. long device_ioctl(struct file *file, /* ditto */
  149. unsigned int ioctl_num, /* number and param for ioctl */
  150. unsigned long ioctl_param)
  151. {
  152. int i;
  153. char *temp;
  154. char ch;
  155. /*
  156. * Switch according to the ioctl called
  157. */
  158. switch (ioctl_num) {
  159. case IOCTL_SET_MSG:
  160. /*
  161. * Receive a pointer to a message (in user space) and set that
  162. * to be the device's message. Get the parameter given to
  163. * ioctl by the process.
  164. */
  165. temp = (char *) ioctl_param;
  166. /*
  167. * Find the length of the message
  168. */
  169. get_user(ch, temp);
  170. for (i = 0; ch && i < BUF_LEN; i++, temp++)
  171. get_user(ch, temp);
  172. device_write(file, (char *) ioctl_param, i, 0);
  173. break;
  174. case IOCTL_GET_MSG:
  175. /*
  176. * Give the current message to the calling process -
  177. * the parameter we got is a pointer, fill it.
  178. */
  179. i = device_read(file, (char *) ioctl_param, 99, 0);
  180. /*
  181. * Put a zero at the end of the buffer, so it will be
  182. * properly terminated
  183. */
  184. put_user('\0', (char *) ioctl_param + i);
  185. break;
  186. case IOCTL_GET_NTH_BYTE:
  187. /*
  188. * This ioctl is both input (ioctl_param) and
  189. * output (the return value of this function)
  190. */
  191. return Message[ioctl_param];
  192. break;
  193. }
  194. return SUCCESS;
  195. }
  196. /* Module Declarations */
  197. /*
  198. * This structure will hold the functions to be called
  199. * when a process does something to the device we
  200. * created. Since a pointer to this structure is kept in
  201. * the devices table, it can't be local to
  202. * init_module. NULL is for unimplemented functions.
  203. */
  204. struct file_operations Fops = {
  205. .read = device_read,
  206. .write = device_write,
  207. .unlocked_ioctl = device_ioctl,
  208. .open = device_open,
  209. .release = device_release, /* a.k.a. close */
  210. };
  211. /*
  212. * Initialize the module - Register the character device
  213. */
  214. int init_module()
  215. {
  216. int ret_val;
  217. /*
  218. * Register the character device (atleast try)
  219. */
  220. ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &Fops);
  221. /*
  222. * Negative values signify an error
  223. */
  224. if (ret_val < 0) {
  225. pr_alert("%s failed with %d\n",
  226. "Sorry, registering the character device ", ret_val);
  227. return ret_val;
  228. }
  229. Major = ret_val;
  230. cls = class_create(THIS_MODULE, DEVICE_FILE_NAME);
  231. device_create(cls, NULL, MKDEV(Major, MAJOR_NUM), NULL, DEVICE_FILE_NAME);
  232. pr_info("Device created on /dev/%s\n", DEVICE_FILE_NAME);
  233. return 0;
  234. }
  235. /*
  236. * Cleanup - unregister the appropriate file from /proc
  237. */
  238. void cleanup_module()
  239. {
  240. device_destroy(cls, MKDEV(Major, 0));
  241. class_destroy(cls);
  242. /*
  243. * Unregister the device
  244. */
  245. unregister_chrdev(Major, DEVICE_NAME);
  246. }
  247. MODULE_LICENSE("GPL");