简体   繁体   中英

How to read/write Character Device within a Linux kernel module

I write the character device driver myself in LKM, which has simply:

dev_open(struct inode *inode, struct file *filp);
dev_read (struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
dev_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
dev_release(struct inode *inode, struct file *filp)

Then in my kernel module, I also want to write to the character device, and the write must actually call my function:

dev_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)

I've found a similar link here at SE, but in this way it won't call my dev_write() function to write but some deeper like vfs_write() ,right?

Don't try to call dev_write() from within your module. You need a separate way for your module to write to the device, if that's really what you want to do. You don't have a valid filp or user memory buffer when you are doing internal writes.

For example dev_write() would use filp to work out which device the user was writing to, and check and copy data from the user buffer. It could call a separate helper function to stick the data into the appropriate internal buffer.

Your internal code would just call that helper function to append the buffer directly. If your driver supports multiple devices, then when you do the internal write you will need to know and specify which instance of the device you are writing to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM