Advertisement
Guest User

Untitled

a guest
Dec 10th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. /*
  2. * chardev.c: Creates a read-only char device that says how many times
  3. * you've read from the dev file
  4. */
  5.  
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/fs.h>
  9. #include <asm/uaccess.h> /* for put_user */
  10.  
  11. /*
  12. * Prototypes - this would normally go in a .h file
  13. */
  14. int init_module(void);
  15. void cleanup_module(void);
  16. static int device_open(struct inode *, struct file *);
  17. static int device_release(struct inode *, struct file *);
  18. static ssize_t device_read(struct file *, char *, size_t, loff_t *);
  19. static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
  20.  
  21. static int len=100;
  22. module_param(len, int, S_IRUGO);
  23. MODULE_PARM_DESC(len, "The length of buffer");
  24.  
  25. #define SUCCESS 0
  26. #define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
  27. #define BUF_LEN 100
  28.  
  29.  
  30. /*
  31. * Global variables are declared as static, so are global within the file.
  32. */
  33.  
  34.  
  35.  
  36. static int Major; /* Major number assigned to our device driver */
  37. static int Device_Open = 0; /* Is device open?
  38. * Used to prevent multiple access to device */
  39. static char msg[BUF_LEN]; /* The msg the device will give when asked */
  40. static char *msg_Ptr;
  41.  
  42. static struct file_operations fops = {
  43. .read = device_read,
  44. .write = device_write,
  45. .open = device_open,
  46. .release = device_release
  47. };
  48.  
  49. /*
  50. * This function is called when the module is loaded
  51. */
  52. int init_module(void)
  53. {
  54. Major = register_chrdev(0, DEVICE_NAME, &fops);
  55.  
  56. if (Major < 0) {
  57. printk(KERN_ALERT "Registering char device failed with %d\n", Major);
  58. return Major;
  59. }
  60.  
  61. printk(KERN_INFO "I was assigned major number %d. To talk to\n", Major);
  62. printk(KERN_INFO "the driver, create a dev file with\n");
  63. printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major);
  64. // minor numbers are used to differentiate multiple instances of a
  65. // device that use the same driver.
  66. printk(KERN_INFO "Try various minor numbers. Try to cat and echo to\n");
  67. printk(KERN_INFO "the device file.\n");
  68. printk(KERN_INFO "Remove the device file and module when done.\n");
  69.  
  70. return SUCCESS;
  71. }
  72.  
  73. /*
  74. * This function is called when the module is unloaded
  75. */
  76. void cleanup_module(void)
  77. {
  78. /*
  79. * Unregister the device
  80. */
  81. #if 0
  82. int ret = unregister_chrdev(Major, DEVICE_NAME);
  83. if (ret < 0)
  84. printk(KERN_ALERT "Error in unregister_chrdev: %d\n", ret);
  85. #endif
  86. unregister_chrdev(Major, DEVICE_NAME);
  87. }
  88.  
  89. /*
  90. * Methods
  91. */
  92.  
  93. /*
  94. * Called when a process tries to open the device file, like
  95. * "cat /dev/mycharfile"
  96. */
  97. static int device_open(struct inode *inode, struct file *file)
  98. {
  99. static int counter = 0;
  100.  
  101. if (Device_Open)
  102. return -EBUSY;
  103.  
  104. Device_Open++;
  105. sprintf(msg, "I already told you %d times Hello world!\n", counter++);
  106. msg_Ptr = msg;
  107. try_module_get(THIS_MODULE);
  108.  
  109. return SUCCESS;
  110. }
  111.  
  112. /*
  113. * Called when a process closes the device file.
  114. */
  115. static int device_release(struct inode *inode, struct file *file)
  116. {
  117. Device_Open--; /* We're now ready for our next caller */
  118.  
  119. /*
  120. * Decrement the usage count, or else once you opened the file, you'll
  121. * never get get rid of the module.
  122. */
  123. module_put(THIS_MODULE);
  124.  
  125. return 0;
  126. }
  127.  
  128. /*
  129. * Called when a process, which already opened the dev file, attempts to
  130. * read from it.
  131. */
  132. static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
  133. char *buffer, /* buffer to fill with data */
  134. size_t length, /* length of the buffer */
  135. loff_t * offset)
  136. {
  137. /*
  138. * Number of bytes actually written to the buffer
  139. */
  140. int bytes_read = 0;
  141.  
  142. /*
  143. * If we're at the end of the message,
  144. * return 0 signifying end of file
  145. */
  146. if (*msg_Ptr == 0)
  147. return 0;
  148.  
  149. /*
  150. * Actually put the data into the buffer
  151. */
  152. while (length && *msg_Ptr) {
  153.  
  154. /*
  155. * The buffer is in the user data segment, not the kernel
  156. * segment so "*" assignment won't work. We have to use
  157. * put_user which copies data from the kernel data segment to
  158. * the user data segment.
  159. */
  160. put_user(*(msg_Ptr++), buffer++);
  161.  
  162. length--;
  163. bytes_read++;
  164. }
  165.  
  166. /*
  167. * Most read functions return the number of bytes put into the buffer
  168. */
  169. return bytes_read;
  170. }
  171.  
  172. /*
  173. * Called when a process writes to dev file: echo "hi" > /dev/hello
  174. */
  175. static ssize_t
  176. device_write(struct file *filp, const char *buffer, size_t length, loff_t * off)
  177. {
  178. int i;
  179.  
  180. #ifdef DEBUG
  181. printk(KERN_INFO "device_write(%p,%s,%d", file, buffer, length)
  182. #endif // DEBUG
  183. for(i=0; (i<length && i<BUF_LEN) || i<len; i++)
  184. {
  185. get_user(msg[i], buffer+i);
  186. }
  187.  
  188. msg_Ptr=msg;
  189. return i;
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement