将套接字代码与 Linux PCI 驱动程序连接
我有两个与 PCI 接口的设备。我还有使用通用套接字代码的两种设备的代码。 (这些设备最初是通过 MII/以太网连接的。)
现在,我需要编写一个 PCI 设备驱动程序来在两个设备之间来回传输数据包。
如何访问套接字代码打开的文件描述符?这与访问字符设备文件相同吗?
PCI 驱动程序必须以某种方式从代码中的 read() 和 write() 捕获数据包。
谢谢!
I have two devices that are interfaced with PCI. I also have code for both devices that uses generic socket code. (The devices were originally connected by MII/Ethernet.)
Now, I need to write a PCI device driver to transport packets back and forth between the two devices.
How do I access the file descriptors opened by the socket code? Is this the same as accessing a character device file?
The PCI driver has to somehow capture packets from read() and write() in the code.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题的答案是:(1) 您不这样做,(2) 不。
文件描述符是一个用户空间概念,内核驱动程序不与用户空间概念交互。 (是的,它们是由内核实现的,但其他设备驱动程序不能直接使用它们,甚至不应该间接使用它们。)
您要做的就是实现接收缓冲数据的方法。内核可访问的内存空间,并将其发送到硬件,然后从硬件接收数据并将其写入(当要求时)到内核可访问内存中的缓冲区。
为此,您将实现字符设备驱动程序 API 和 PCI 设备驱动程序 API,然后将驱动程序注册为 PCI 设备,然后注册为字符设备。虽然其中一些方法可能引用文件结构,但它们不会是您了解和喜爱的用户态结构。
对于实现以太网协议的设备,生活会更容易,因为您实现了网络设备接口。这样,您只需编写从硬件获取数据所需的部分即可。
您需要的是设备硬件的规范、如何使用 PCI 寄存器和区域控制硬件。
好消息是,您不必独自完成这件事——有一个庞大的内核开发人员社区,以及几本关于 Linux 内核开发的好(且最新)书籍(见下文)。
参考资料
The answers to your questions are: (1) You don't, and (2) no.
File descriptors are a user-space concept, and kernel drivers don't interact with user-space concepts. (Yes, they're implemented by the kernel, but other device drivers don't get to play with them directly, and shouldn't play with them even indirectly.)
What you do is implement methods that will receive data that is buffered in a kernel-accessible memory space, and send that to your hardware, and then receive data from your hardware and write it (when asked) to a buffer in kernel-accessible memory.
You'll do this by implementing the character device driver APIs as well as the PCI device driver APIs and then registering your driver as a PCI device, and then a character device. While some of these methods may refer to file structures, they will not be the user-land structures that you know and love.
For devices that implement the Ethernet protocols, life is easier because you implement the Net Device Interface instead. This way all you have to write is the parts necessary to get data to and from your hardware.
What you'll need is specifications for the device hardware, how you control the hardware using PCI registers and regions.
The good news is, you don't have to do this alone -- there's a large community of kernel developers, and several good (and current) books on developing for the Linux kernel (see below).
References