将数据流式传输到 Linux 设备驱动程序中的文件中
我对 Linux 设备驱动程序编程相当陌生,但出于调试目的,我想将数据流直接写入文件。或者也许我应该以不同的方式问这个问题:
我有一个片上系统,其中一个模块提供数据流。该模块必须使用写入地址进行初始化。我不想将数据写入内存,而是将数据流重定向到文件的该地址。这也可以通过用户空间完成,因为不建议在内核空间中写入文件。
有人可以粗略地概括一下如何做到这一点吗?
谢谢,斯特凡
I am rather new to Linux device driver programmring, but for debugging purposes, I would like to write a stream of data directly to a file. Or maybe I should aks the question differently:
I have a system-on-chip, where one module provides a data stream. The module has to be initlized with a write address. Instead of writing the data into memory, I would like to redirect the data stream to that address to a file. This could also be done via userspace, because writing to file in kernelspace is not recommended.
Can somebody sketch roughly how to do this?
Thanks, Stefan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从你的问题来看,我认为这是一个直接写入内存缓冲区的硬件模块。在访问文件的过程中,您始终需要一个中间内存缓冲区,但有多种方法可以在设备驱动程序中管理它。
最简单的情况是实现一个字符设备驱动程序,它通过读取操作将数据返回到用户空间。写入该文件的用户空间应用程序可以像
cat
一样简单。您的模块将写入由驱动程序分配的内存缓冲区,并且 read 方法会将数据从该缓冲区复制到用户空间缓冲区。内核文档和 Linux 设备驱动程序中的DMA-Mapping.txt
对此进行了介绍(LDD)。如果数据速率很高(其中高与设备的性能相关),您需要考虑更高级的读取操作,将
读取
请求的页面锁定到内存中,并且模块直接写入这些页面。这比较复杂,LDD 中关于这方面的文档相当旧。我建议研究内核中视频捕获驱动程序的源代码以了解这一点。如果您的驱动程序需要执行更多类型的通信,您可能需要使用 Netlink 。
From your question, I assume this is a hardware module writing directly into a memory buffer. You will always need an intermediate memory buffer on the way to the file, but there are several ways to manage this in your device driver.
The simplest case is to implement a character device driver, which returns the data to userspace via a read operation. Your userspace application that writes to the file can then be as simple as
cat
. Your module will write into a memory buffer allocated by your driver, and theread
method will copy data from this buffer to the userspace buffer. This is covered byDMA-Mapping.txt
in the kernel documentation and in Linux Device Drivers (LDD).If the data rate is high (where high is relative to the performance of the device), you'll need to consider a more advanced read operation, where you lock the pages of the
read
request into memory, and the module writes directly to those pages. This is more complicated, and the documentation in LDD on this area is quite old. I'd advise studying the source of the video capture drivers in the kernel to understand this.If your driver has more sorts of communication to carry out, you may want to use Netlink.