4

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?

Community
  • 1
  • 1
keywind
  • 1,135
  • 14
  • 24

1 Answers1

1

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.

blueshift
  • 6,742
  • 2
  • 39
  • 63
  • 1
    In particular, `dev_write` must use the `copy_from_user` class of functions to copy data safely from userspace, which will prevent it from copying data from kernelspace buffers. You could, in principle, set up a faux userspace context to work with, but it's easier to just create a backdoor for yourself. – bdonlan Apr 22 '12 at 02:56
  • 1
    @bdonlan I'm new to linux kernel programming, would you please give a detailed instruction about how to set up a faux userspace context to work with or create a backdoor – keywind Apr 22 '12 at 07:23