gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:01:49 25 4
gpt4 key购买 nike

自定义读写操作定义为

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

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

最佳答案

@user567879,由于设备节点被视为特殊字符或 block 或网络文件,每个文件都有一个文件结构“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)

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

关于linux - 设备驱动程序如何写入/读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19294620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com