I have a Linux kernel module that implements a character device driver. I've read through Linux Device Drivers and followed several tutorials. At this point, I have a simple module that provides open
, release
, and write
file operations.
I'm trying to use the Generic DMA Layer to create a streaming DMA mapping. I'm confused by the following excerpt from LDD:
Many of the functions below require a
struct device
. This structure is the low-level representation of a device within the Linux device model. It is not something that drivers often have to work with directly, but you do need ot when using the generic DMA layer. Usually, you can find this structure buried inside the bus specific that describes your device. For example, it can be found as thedev
field instruct pci_device
orstruct usb_device
.
I read further into the Linux device model, and encountered the following:
At the lowest level, every device in a Linux system is represented by an instance of struct device.
How can I get the struct device
for my character device? Is there one being created for me behind the scenes, or do I need to create it?
I tried manually creating a class with class_create()
and then using that to create a device with device_create()
, but when I used that device to set up DMA mappings I think I just got a bogus address. Is this the correct approach?
For a little bit more information about my platform, I'm working on the Altera SoCFPGA platform (ARM), so my device isn't a true hardware device like a USB or PCI device, but rather logic implemented in an FPGA.
I found a lot of info in Chapter 14 of LDD that I think may be relevant (buses, devices, drivers, etc.), but I'm just not sure when or how to use it. To me, it seems like that chapter is discussing a lot of data structures that all devices and drivers use, but I'm confused because I haven't had to make use of any of it.
Character devices are devices that do not have physically addressable storage media, such as tape drives or serial ports, where I/O is normally performed in a byte stream.
At the lowest level, every device in a Linux system is represented by an instance of struct device. The device structure contains the information that the device model core needs to model the system. Most subsystems, however, track additional information about the devices they host.
Examples of devices using character drivers include tape drives and serial ports. Character device drivers can also provide additional interfaces not present in block drivers, such as I/O control (ioctl) commands, memory mapping, and device polling.
I ended up creating a platform driver and platform device. The platform device struct has it's own struct device
associated with the platform bus, which is a 'pseudo-bus' designed exactly for things like this. The official documentation for platform drivers was helpful here.
In the end, my module ended up implementing both a platform driver and a character device driver. The part that gave me the most trouble was creating a platform device and associating it with my platform driver. I started by just manually creating the device (at module install time) with platform_device_alloc
and platform_device_register
. Once I had this working I ended up removing the manual device creation and instead relying on a device tree entry to create my device.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With