How do you get a struct device for a Linux character device
Asked Answered
A

4

6

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 the dev field in struct pci_device or struct 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.

Afoot answered 3/3, 2015 at 16:21 Comment(0)
A
2

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.

Afoot answered 16/6, 2016 at 19:3 Comment(0)
N
1

It depends. In one cases you may get device created by core, in other you have to do that. (I think you are at first group)

Device pointer you are using for DMA should represent the device which does actual DMA in hardware. So, your approach is wrong there.

It might be good to pre-order LDD4.

Nitpicking answered 3/3, 2015 at 20:45 Comment(1)
Can you elaborate on get device created by core?Afoot
C
1

I ran into exactly this as well. This is hardly a complete answer, but it seems that the underlying assumption in most of these docs is that basic character devices (that aren't instantiated by some higher-level bus driver etc.) won't need access to DMA. I found another shortcut to get what I needed without a struct device, but it looked like you could create your own device using device_register() or similar.

Culpa answered 15/6, 2016 at 22:24 Comment(1)
Thanks for your answer - it reminded me to jump back on here and post an update!Afoot
A
0

It is maybe a six years old the answer, but because you are doing your own driver, you could later use the function device_create from Linux header include/linux/device.h to create dynamically not only your device in /dev/ folder, but it would also return a pointer to a struct device that you can use later on in the creation of a DMA mapping as the book suggest.

I found particular useful this video in case that the information of the book is not enough to get the variables that you need for the device_create function. How to create a Linux Character Driver

Auricula answered 17/1, 2022 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.