Reputation: 1998
I'm trying to write a simple device driver, and use the Dev_Read() and Dev_Write() functions, which I have already defined. My driver registers a character device tied to a given major number this way
# mknod /dev/mydev c 250 0
And then, from my user program I open the device this way
int fd;
if ((fd = open("/dev/mydev", O_RDWR)) < 0)
{
perror("open /dev/mydev");
exit(EXIT_FAILURE);
}
after I invoke the Read() function
int read_bytes = read (fd, buffer, 1);
and successfully get kernel space information, that my Dev_Read function works. My problem is that I don't understand how to implement my Dev_Write function. How can I write someting to my file, to see that Dev_Write function works ? THANK YOU for help.
Upvotes: 2
Views: 1764
Reputation: 39807
It would help to know what you've tried which didn't work.
One thing to be aware of, and not necessarily intuitive, is that your driver's write function must copy the write buffer from the caller's space into kernel space. An example of that can be seen in the tutorial at http://www.freesoftwaremagazine.com/articles/drivers_linux -
ssize_t memory_write( struct file *filp, char *buf,
size_t count, loff_t *f_pos) {
char *tmp;
tmp=buf+count-1;
copy_from_user(memory_buffer,tmp,1);
return 1;
}
where memory_buffer
is space you've allocated within your driver.
Upvotes: 3