Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<linux/init.h>
- #include<linux/module.h>
- #include<linux/fs.h>
- #include<linux/slab.h>
- #include<linux/uaccess.h>
- #define BUFFER_SIZE 1024
- /* Define device_buffer and other global data structures you will need here */
- int open_count = 0;
- int closed_count = 0;
- void* device_buffer;
- int buffer_offset = 0;
- ssize_t simple_char_driver_read (struct file *pfile, char __user *buffer, size_t length, loff_t *offset)
- {
- /* *buffer is the userspace buffer to where you are writing the data you want to be read from the device file*/
- /* length is the length of the userspace buffer*/
- /* offset will be set to current position of the opened file after read*/
- /* copy_to_user function: source is device_buffer and destination is the userspace buffer *buffer */
- copy_to_user(buffer, device_buffer, length);
- *offset = buffer_offset;
- return 0;
- }
- ssize_t simple_char_driver_write (struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)
- {
- /* *buffer is the userspace buffer where you are writing the data you want to be written in the device file*/
- /* length is the length of the userspace buffer*/
- /* current position of the opened file*/
- /* copy_from_user function: destination is device_buffer and source is the userspace buffer *buffer */
- copy_from_user(device_buffer, buffer, length);
- *offset = buffer_offset;
- printk(KERN_ALERT "[3753] Copied %zu bytes.", length);
- return length;
- }
- int simple_char_driver_open (struct inode *pinode, struct file *pfile)
- {
- /* print to the log file that the device is opened and also print the number of times this device has been opened until now*/
- printk(KERN_ALERT "[3753] Device is opened. Count: %i", open_count);
- return 0;
- }
- int simple_char_driver_close (struct inode *pinode, struct file *pfile)
- {
- /* print to the log file that the device is closed and also print the number of times this device has been closed until now*/
- printk(KERN_ALERT "[3753] Device is closed. Count: %i", closed_count);
- return 0;
- }
- loff_t simple_char_driver_seek (struct file *pfile, loff_t offset, int whence)
- {
- /* Update open file position according to the values of offset and whence */
- switch(whence) {
- case SEEK_SET:
- buffer_offset = offset;
- break;
- case SEEK_CUR:
- buffer_offset += offset;
- break;
- case SEEK_END:
- buffer_offset = BUFFER_SIZE - offset;
- break;
- }
- return 0;
- }
- struct file_operations simple_char_driver_file_operations = {
- .owner = THIS_MODULE,
- /* add the function pointers to point to the corresponding file operations. look at the file fs.h in the linux souce code*/
- .read = simple_char_driver_read,
- .write = simple_char_driver_write,
- .open = simple_char_driver_open,
- .release = simple_char_driver_close,
- .llseek = simple_char_driver_seek
- };
- static int simple_char_driver_init(void)
- {
- /* print to the log file that the init function is called.*/
- printk(KERN_ALERT "[3753] Init function called.");
- /* register the device */
- register_chrdev(240, "Simple Char Driver", &simple_char_driver_file_operations);
- /* allocate the buffer */
- device_buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
- return 0;
- }
- static void simple_char_driver_exit(void)
- {
- /* print to the log file that the exit function is called.*/
- printk(KERN_ALERT "[3753] Exit function called.");
- /* unregister the device using the register_chrdev() function. */
- unregister_chrdev(240, "Simple Char Driver");
- /* free the buffer */
- kfree(device_buffer);
- }
- /* add module_init and module_exit to point to the corresponding init and exit function*/
- module_init(simple_char_driver_init);
- module_exit(simple_char_driver_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement