Results 1 to 1 of 1
I try to write a device driver which when i load module user will write something to to my device like: For example my device file is DFILE under /dev, ...
- 01-04-2008 #1Just Joined!
- Join Date
- Dec 2005
- Posts
- 6
Device driver for a virtual character device
I try to write a device driver which when i load module user will write something to to my device like: For example my device file is DFILE under /dev, user will enter echo "this is my try" > /dev/DFILE
After that user will enter
cat /proc/NEWD
We expected to see what we write.
My Code is below:
What is wrong with this. How can i write to for example to /dev/DFILE :
Code:#include <linux/module.h> #include <linux/kernel.h> #include <linux/proc_fs.h> #include <asm/uaccess.h> /*This holds information of the /proc/mycookie file */ static struct proc_dir_entry *mydeviceFile; /*This buffer stores all the text as characters which user entered*/ static char myBuffer[100000]; /*Holds size of the buffer*/ static unsigned long myBufferSize = 0; int procfile_read(char *buffer,char **buffer_location, off_t offset, int buffer_length, int *eof, void *data) { int ret; printk(KERN_INFO "procfile_read (/proc/%s) called\n", "NEWD"); if (offset > 0) { /* we have finished to read, return 0 */ ret = 0; } else { /* fill the buffer, return the buffer size */ memcpy(buffer, myBuffer, myBufferSize); ret = myBufferSize; } return ret; } int procfile_write(struct file *file, const char *buffer, unsigned long count,void *data) {/* This function is called with the /proc file is written*/ myBufferSize = count;/* get buffer size */ if (myBufferSize > 100000 ) { myBufferSize = 100000; } if ( copy_from_user(myBuffer, buffer, myBufferSize) )/*Stores all characters of the cookie list in myBuffer*/ { return -EFAULT; } return myBufferSize; } int init_module() { /*When the module is loaded, this function is called and creates the /proc/NEWD*/ mydeviceFile = create_proc_entry("NEWD", 0644, NULL); if (mydeviceFile == NULL) { remove_proc_entry("NEWD", &proc_root); printk(KERN_ALERT "Error:Module cannot be initialized /proc/%s\n","NEWD"); return -ENOMEM; } mydeviceFile->write_proc = procfile_write; mydeviceFile->owner = THIS_MODULE; mydeviceFile->mode = S_IFREG | S_IRUGO; mydeviceFile->uid = 0; mydeviceFile->gid = 0; mydeviceFile->size = 100000; printk(KERN_INFO "/proc/%s is created\n", "NEWD"); return 0; } /*This function is called when the module is unloaded*/ void cleanup_module() { remove_proc_entry("NEWD", &proc_root); printk(KERN_INFO "/proc/%s is removed\n", "NEWD"); }


Reply With Quote
