Linux驱动程序中的结构文件
我目前正在学习如何编写 Linux 设备驱动程序,但我无法理解“struct file”。我正在使用 Linux Device Drivers 第三版 这本书来帮助我。
这是我的理解。
一个。 struct file代表一个打开的文件,因此,当在设备驱动模块中调用open时,内核将创建一个struct file,其中包含与设备驱动程序相关的所有内容。
b.如果您想传递设备驱动程序的此实例,则必须传递一个指向特定结构文件的指针,该指针是在 open()
c 之后由内核创建的。 file->private_data 将始终返回指向设备的指针。
与此相关的另一个问题是字段“f_pos”。书上说驱动程序如果想知道文件中的当前位置可以读取这个值。这是我从中了解到的。
d.如果struct foo_dev并且该驱动程序用于存储数据的内存总量为X,则f_pos指向该块中的当前位置驱动程序保留的内存。
我的理解有多少是对的,有错误的地方还请指正。
谢谢,
米尔
I am currently learning how to write Linux device drivers and I have trouble understanding "struct file". I am using the book Linux Device Drivers 3rd edition to help me out.
This is what I understood.
a. struct file represents an open file thus, when open is called in the device driver module, the kernel will create a struct file that includes everything related to the device driver.
b. If you want to pass around this instance of the device driver then one has to pass a pointer to the particular struct file that was created by the kernel after open()
c. file->private_data will always return a pointer to the device.
Another question related to this is the field "f_pos". The book says that the driver can read this value if it wants to know the current position in the file. This is what I understand from it.
d. If struct foo_dev and if the total amount of memory used by this driver to store data is X then f_pos points to the current position in that block of memory reserved by the driver.
How much of what I understood is right and please correct me where I am wrong.
Thanks,
Mir
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结构文件由内核创建,代表设备的内核视图,它允许内核从文件句柄映射到设备。
结构文件仅包含内核上层所需的数据,这不太可能是您的驱动程序所需的一切,如果您需要额外的存储来跟踪设备状态(通常您会),您需要为结构分配内存您自己可以在打开功能中或更正常地在检测到硬件时进行操作。
如果您确实分配存储,那么您可以使用 file->private_data 来允许您通过读/写等方式从传递给驱动程序的结构文件中获取结构。
如何使用 file->private_data 由驱动程序决定,内核不涉及它。它仅供驾驶员使用。
f_pos 字段是内核遗留下来的,对设备和文件使用相同的结构文件。它是下一个操作将发生的文件的索引,这取决于您的设备是否有意义,如果您的设备支持某种形式的随机访问(例如 RAM 设备),那么使用 f_pos 并实现 lseek 可能有意义,如果如果你的硬件是顺序的,那么 f_pos 通常是不相关的。
The struct file is created by the kernel and represents the kernels view of your device it allows the kernel to map from a file handle to the device.
The struct file only contains the data the kernels upper layers needs, this is unlikely to be everything you need for your driver, if you need extra storage to track your devices status (and generally you will) you need to allocate the memory for your structure yourself either in the open function or more normally when you detect your hardware.
If you do allocate storage then you can use the file->private_data to allow you to get from the struct file thats passed to your driver by read / write / etc to your structure.
How the file->private_data is used is up to the driver, the kernel doesn't touch it. Its just there for the drivers use.
The f_pos field is a legacy from the kernel using the same struct file for devices and files. It is an index into a file were the next operation will happen, it depends on your device if this makes sense, if your device supports some form of random access (say a ram device) then using f_pos and implementing lseek might make sense, if you hardware is sequential then f_pos is normally irrelevant.
这是对 andrew 所说的的补充……
a) struct FILE 由内核提供,但它是内核和一个应用程序之间的接口。
b) 换句话说,您不能在共享设备的多个应用程序之间传递 FILE 结构。唯一可以共享的例外是在父母和孩子之间。子进程。
要从多个应用程序(每个应用程序)同时访问设备或设备驱动程序。必须在设备上调用 open创建自己的 FILE 结构。是否允许同时访问由驱动程序决定。内核在这里没有发言权。
c) private_data 正是它所说的。设备驱动程序私有的数据。应用程序或库可以使用此字段来传达设备驱动程序特定的数据。
This is in addition to what andrew has said ...
a) struct FILE is provided by kernel, but it is meant as an interface between kernel and one application.
b) In other words, you cannot pass around FILE structure between multiple applications for sharing a device. The only exception where it is possible to share is between parent & child processes.
To access a device or device drivers simultaneously from multiple applications, each app. shall have to call open on the device & create a FILE struct of its own. It is up to the driver whether to allow simultaneous accesses or not. Kernel has no say here.
c) private_data is exactly what it says. Data that's private to device driver. Application or library can use this field to communicate data that is very specific for the device driver.