【问题标题】:How device driver write/read works设备驱动程序如何写入/读取
【发布时间】:2013-10-10 11:31:41
【问题描述】:

自定义读写操作定义为

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

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

【问题讨论】:

  • 从LDD的解释中你到底有什么不明白的?
  • (struct file *,char __user *, size_t, loff_t *); ?将字符串写入文件时会发生什么?
  • 调用write函数,参数值在LDD中描述。

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


【解决方案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)

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

【讨论】:

  • 这些函数的参数是什么?
  • 你指的是哪些函数?是设备驱动程序中的系统调用参数还是映射函数?
  • (struct file *,char __user *, size_t, loff_t *); ?将字符串写入文件时会发生什么?
  • 首先会检查打开的文件是只读的还是可读写的,如果是可读写的,则用户空间应用程序传递的字符串然后排队,然后在write的帮助下将字符串写入指定的文件系统调用又调用设备驱动程序映射的写入函数。写入文件不会立即发生,因为它涉及硬件(I/O 操作),而且写入/读取到/从存储设备非常缓慢且按顺序进行。块设备驱动架构将更好地向您解释文件的读/写。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 2012-11-29
  • 2023-03-23
相关资源
最近更新 更多