在Linux驱动程序文件结构 [英] struct file in linux driver

查看:180
本文介绍了在Linux驱动程序文件结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在学习如何编写Linux设备驱动程序和我有麻烦理解结构文件。我现在用的书Linux设备驱动程序第三版来帮助我。

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.

这是我的理解。

一个。 结构文件再presents打开的文件,因此,当打开被称为设备驱动程序模块,内核将创建包括与设备驱动程序一切结构文件。

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()

℃。 文件 - > private_data的总是会返回一个指向该设备。

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

与此相关的另一个问题是该领域 f_pos 。书上说的,如果它想知道文件在当前位置的驱动程序可以读取该值。这是我从中明白了。

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.

Ð。如果结构foo_dev ,如果所使用的内存此驱动程序存储的数据总量 X ,然后f_pos指向该块的当前位置的存储器由驾驶员保留。

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

推荐答案

该结构文件是由内核并重新$ P $创建psents设备的内核认为它可以让内核从文件句柄映射到设备。

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.

如果你分配存储空间,那么你可以使用文件 - > private_data是让你能够从读/写/等传递给你的驱动程序到你的结构结构的文件多数民众赞成获得。

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.

文件 - > 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.

该f_pos场使用的设备和文件相同的文件结构从内核的遗产。这是一个索引文件是下一个操作会发生,这取决于您的设备上,如果这是有道理的,如果你的设备支持某种形式的随机访问(比如一个RAM器件),然后使用f_pos和实施lseek的可能有意义,如果你的硬件是连续的,则f_pos通常是无关紧要的。

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.

这篇关于在Linux驱动程序文件结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆