设备驱动程序如何写入/读取

时间:2013-10-10 11:31:41

标签: linux linux-kernel driver linux-device-driver device-driver

自定义读写操作定义为

ssize_t (*read) (struct file *,char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *,const char __user *, size_t, loff_t *);

对设备进行读写操作会发生什么?

我在LDD书中找不到这个简单的解释。

例如,当我有一个设备并且我做了像

这样的写操作时会发生什么
echo "Hello" > /dev/newdevice

我正在写一个简单的角色设备。还

cat  /dev/newdevice

我知道这取决于我的自定义读/写,我需要的是从内存中简单读取并写入内存

1 个答案:

答案 0 :(得分:1)

@ user567879,由于设备节点被视为特殊字符或块或网络文件,因此每个文件都有一个文件结构“filp”,后者依次保存指向文件的指针操作表其中每个系统调用都映射到设备驱动程序中的相应函数。

for ex: .open = my_open
        .write = my_write 
        .read = my_read etc.

发出echo“Hello”>时会发生什么? / dev / newdevice是

1) Device node i.e. "/dev/newdevice" is opened using open system call which in turn 
   calls your mapped open function i.e. "**my_open**"

2) If open is successful, write system call issued with appropriate file descriptor 
   (fd), which in turn calls "**my_write**" function present in device driver and thus 
   according to the functionality it writes/transmits user data to the actual 
   hardware. 

3) Same rule applies for "cat  /dev/newdevice" i.e. open the device node --> read 
   system call --> mapped read function in your device driver i.e. "**my_read**" --> 
   reads the data from actual hardware and sends the data read from the hardware to
   user space (application which issued read system call)

我希望我已经回答了你的问题: - )