linux驱动中的struct文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4653100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:25:36  来源:igfitidea点击:

struct file in linux driver

clinuxkerneldriverlinux-device-driver

提问by Mir

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.

我目前正在学习如何编写 Linux 设备驱动程序,但我无法理解“结构文件”。我正在使用 Linux Device Drivers 3rd edition 这本书来帮助我。

This is what I understood.

这是我的理解。

a. struct filerepresents 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.

一种。struct file代表一个打开的文件,因此,当在设备驱动程序模块中调用 open 时,内核将创建一个包含与设备驱动程序相关的所有内容的结构文件。

b. If you want to pass around this instance of the device driver then one has to pass a pointer to the particular struct filethat was created by the kernel after open()

湾 如果你想传递设备驱动程序的这个实例,那么你必须传递一个指向由内核在 open() 之后创建 的特定结构文件的指针

c. file->private_datawill always return a pointer to the device.

C。file->private_data将始终返回指向设备的指针。

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.

与此相关的另一个问题是“ f_pos”字段。书中说如果驱动程序想知道文件中的当前位置,可以读取这个值。这是我从中了解到的。

d. If struct foo_devand if the total amount of memory used by this driver to store data is Xthen f_pos points to the current position in that block of memory reserved by the driver.

d. 如果struct foo_dev并且此驱动程序用于存储数据的内存总量为X,则 f_pos 指向驱动程序保留的该内存块中的当前位置。

How much of what I understood is right and please correct me where I am wrong.

我的理解有多少是正确的,请纠正我的错误。

Thanks,
Mir

谢谢,
米尔

回答by Andrew Roca

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.

struct 文件由内核创建,代表设备的内核视图,它允许内核从文件句柄映射到设备。

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.

如果您确实分配了存储,那么您可以使用 file->private_data 来允许您从通过读取/写入/等传递给您的驱动程序的结构文件中获取您的结构。

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.

如何使用 file->private_data 取决于驱动程序,内核不会触及它。它只是供司机使用。

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.

f_pos 字段是内核遗留下来的,对设备和文件使用相同的结构文件。它是下一个操作将发生的文件索引,这取决于您的设备是否有意义,如果您的设备支持某种形式的随机访问(例如 ram 设备),那么使用 f_pos 并实现 lseek 可能是有意义的,如果您的硬件是连续的,然后 f_pos 通常无关紧要。

回答by philosopher.stoned

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.

a) struct FILE 由内核提供,但它意味着内核和一个应用程序之间的接口。

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.

b) 换句话说,你不能在多个应用程序之间传递 FILE 结构来共享一个设备。可以共享的唯一例外是在父进程和子进程之间。要从多个应用程序同时访问一个设备或设备驱动程序,每个应用程序。必须在设备上调用 open 并创建自己的 FILE 结构。是否允许同时访问取决于驱动程序。内核在这里没有发言权。

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.

c) private_data 正是它所说的。设备驱动程序私有的数据。应用程序或库可以使用此字段来传达非常特定于设备驱动程序的数据。