Stream data into a file in a linux device driver
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
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 the read
method will copy data from this buffer to the userspace buffer. This is covered by DMA-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.
精彩评论