I have to write a program in c or c++ for Device driver. It should open, read, write and excute. so far I have come up with these. if anybody know any links that can help me to write it or anybody know the actual program plz help me . it is very important to me. Thanks in advance. // Header files needed to load the driver #include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #DEFINE BUFF_SIZE 16 MODULE_AUTHOR("OS student"); MODULE_DESCRIPTION("Box's assignment"); static int hari_open = 0; static char buff_size[BUFF_SIZE]; static int index = 0; static char *DEVICE_NAME = "hari"; static int major_no = 0; struct file_operations fops = { read: hari_read, write: hari_write, open: hari_open, release: hari_release }; int hari_open(struct inode *inode, struct file *file) { if (hari_open) { // Allowing only one process to open this device at a time. return -EBUSY; } hari_open++; // This will prevent rmmod to successfully remove // this module when the device is in use. try_module_get(THIS_MODULE); // on open, reset the read index to 0 index = 0; return 0; } int hari_release(struct inode *inode, struct file *file) { hari_open--; // releaseing this module so an admin may rmmod it. module_put(THIS_MODULE); return 0; } ssize_t hari_read(struct file *f, char *buffer, size_t length, loff_t *offset) { int i; int bytes_read = 0; // This will just simply copy device buffer to the reader for(i = 0; index < BUFF_SIZE && i < length; i++) { buffer[i] = box_buff[index]; index++; bytes_read++; } return bytes_read; } ssize_t hari_write(struct file *f, const char *buffer, size_t length, loff_t *offset) { // This will just overwrite stuffs to the buffer // but the device will not reset the index everytime the 'hari_write' // is called, resulting in writing only at maximum of 16 bytes // on each open. // (a little bit different from // what I showed in the class where we can write 17 bytes // per one open -- with automatic two writes by system) // Feel free to modify this to match the assignment requirement. int bytes_written = 0; int i; for(i = 0; index < BUFF_SIZE && i < length; i++) { box_buff[index++] = buffer[i]; bytes_written++; } if(bytes_written == 0) { // We cannot write anything here ... // meaning that the buffer is full :) // return error return -ENOSPC; // no space left // NOTE: please see 'man errno' for more information // you may change this to some other error as you see fit. } // if the device can write something, return the number of written bytes. return bytes_written; } int init_module(void) { int i; printk(KERN_INFO "DEVICE: module initialization\n"); // initializing the device buffer for (i = 0; i < BUFF_SIZE; i++) { box_buff[i] = 'a'; } // registering device ... if the major number 999 has been occupied, // the register_chrdev function will fail (see messages in // /var/log/kern.log for Ubuntu; please note that the kernel message // can be stored in different location, depending on the system // you're using) major_no = register_chrdev(999, DEV_NAME, &fops); if (major_no < 0) { printk(KERN_ALERT "Registering virtual device failed with %d\n", major_no); return major_no; } return 0; } void cleanup_module() { printk(KERN_INFO "DEVICE: Unloaded\n"); // Finally, don't forget to unregister the device ... or you will not // be able to insmod your module again. unregister_chrdev(999, DEV_NAME); }
codeme · May 2, 2012 10:33 AM · 1,857 views
This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.
Start a New Discussion