Joshua
Joshua

Reputation: 525

Does a kernel driver implementing mmap() have to create a character device?

I am trying to write a kernel driver to manage some memory blocks of physically contiguous and DMAable memory (I am using kmalloc() since these are only DMA streams). To pull some functionality into userspace, this memory is to be mmap()ed with its own implementation of mmap(). I have been using Linux Device Drivers and the bad examples that show up in Google as my main source of information.

My mmap() (calling it my_mmap() for now) needs to be registered with the kernel. It appears the only valid way to do this using a struct file_operations, but this requires creating a character device and a physical location for it. I do not want to do that. I just want to create a virtual address for the userspace application to access the memory buffers and not create any files to map the memory buffers to. Is this possible?

I did find that frame buffers also have an equivalent structure with an mmap() implementation, but that would be too much of a hack. That and it adds more unknowns.

As I understand it, my_mmap() can do the heavy lifting and use remap_pfn_range() as long as I am ok with the lost flexibility. Otherwise I would have to implement a local nopages() and register it using a struct vm_operations_struct. Is this correct?

Upvotes: 4

Views: 4507

Answers (1)

caf
caf

Reputation: 239331

The mmap() operation is a request from userspace to map some source into its virtual address space. The way that the userspace program identifies which source it is interested in is by supplying a file descriptor (which is really just a handle on a resource known to the kernel).

This means that you have to make your device representable as a file descriptor so that a userspace program can tell the kernel that it's interested in it (and the kernel knows to call your mmap() implementation) - registering a character device is the typical way to do that. Note that frame buffer devices are accessed through character devices, too.

You do not have to implement other character device operations like read() and write() if it does not make sense for your device. A character device is simply a way for a userspace program to open a kernel-managed handle to the device.

Upvotes: 6

Related Questions