Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1.  
  2.  
  3. #include<linux/init.h>
  4. #include<linux/module.h>
  5.  
  6. #include<linux/fs.h>
  7. #include<linux/slab.h>
  8. #include<linux/uaccess.h>
  9.  
  10. #define BUFFER_SIZE 1024
  11.  
  12. /* Define device_buffer and other global data structures you will need here */
  13. int open_count = 0;
  14. int closed_count = 0;
  15.  
  16. void* device_buffer;
  17. int buffer_offset = 0;
  18.  
  19. ssize_t simple_char_driver_read (struct file *pfile, char __user *buffer, size_t length, loff_t *offset)
  20. {
  21. /* *buffer is the userspace buffer to where you are writing the data you want to be read from the device file*/
  22. /* length is the length of the userspace buffer*/
  23. /* offset will be set to current position of the opened file after read*/
  24. /* copy_to_user function: source is device_buffer and destination is the userspace buffer *buffer */
  25. copy_to_user(buffer, device_buffer, length);
  26. *offset = buffer_offset;
  27.  
  28. return 0;
  29. }
  30.  
  31.  
  32.  
  33. ssize_t simple_char_driver_write (struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)
  34. {
  35. /* *buffer is the userspace buffer where you are writing the data you want to be written in the device file*/
  36. /* length is the length of the userspace buffer*/
  37. /* current position of the opened file*/
  38. /* copy_from_user function: destination is device_buffer and source is the userspace buffer *buffer */
  39. copy_from_user(device_buffer, buffer, length);
  40. *offset = buffer_offset;
  41.  
  42. printk(KERN_ALERT "[3753] Copied %zu bytes.", length);
  43.  
  44. return length;
  45. }
  46.  
  47.  
  48. int simple_char_driver_open (struct inode *pinode, struct file *pfile)
  49. {
  50. /* print to the log file that the device is opened and also print the number of times this device has been opened until now*/
  51. printk(KERN_ALERT "[3753] Device is opened. Count: %i", open_count);
  52.  
  53. return 0;
  54. }
  55.  
  56. int simple_char_driver_close (struct inode *pinode, struct file *pfile)
  57. {
  58. /* print to the log file that the device is closed and also print the number of times this device has been closed until now*/
  59. printk(KERN_ALERT "[3753] Device is closed. Count: %i", closed_count);
  60.  
  61. return 0;
  62. }
  63.  
  64. loff_t simple_char_driver_seek (struct file *pfile, loff_t offset, int whence)
  65. {
  66. /* Update open file position according to the values of offset and whence */
  67. switch(whence) {
  68. case SEEK_SET:
  69. buffer_offset = offset;
  70. break;
  71. case SEEK_CUR:
  72. buffer_offset += offset;
  73. break;
  74. case SEEK_END:
  75. buffer_offset = BUFFER_SIZE - offset;
  76. break;
  77. }
  78.  
  79. return 0;
  80. }
  81.  
  82. struct file_operations simple_char_driver_file_operations = {
  83.  
  84. .owner = THIS_MODULE,
  85.  
  86. /* add the function pointers to point to the corresponding file operations. look at the file fs.h in the linux souce code*/
  87. .read = simple_char_driver_read,
  88. .write = simple_char_driver_write,
  89. .open = simple_char_driver_open,
  90. .release = simple_char_driver_close,
  91. .llseek = simple_char_driver_seek
  92. };
  93.  
  94. static int simple_char_driver_init(void)
  95. {
  96. /* print to the log file that the init function is called.*/
  97. printk(KERN_ALERT "[3753] Init function called.");
  98.  
  99. /* register the device */
  100. register_chrdev(240, "Simple Char Driver", &simple_char_driver_file_operations);
  101.  
  102. /* allocate the buffer */
  103. device_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
  104.  
  105. return 0;
  106. }
  107.  
  108. static void simple_char_driver_exit(void)
  109. {
  110. /* print to the log file that the exit function is called.*/
  111. printk(KERN_ALERT "[3753] Exit function called.");
  112.  
  113. /* unregister the device using the register_chrdev() function. */
  114. unregister_chrdev(240, "Simple Char Driver");
  115.  
  116. /* free the buffer */
  117. kfree(device_buffer);
  118. }
  119.  
  120. /* add module_init and module_exit to point to the corresponding init and exit function*/
  121. module_init(simple_char_driver_init);
  122. module_exit(simple_char_driver_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement