FILE OPERATIONS(9)
π FILE OPERATIONS π
okay bestie, welcome to file operations β section 9 of the Kernel API Manual β and no, that's not a typo, we're in kernel-land now. grab a strawberry drink π and let's get into it.
So. Drivers must define the file operations structure that forms the DRM userspace API entry point, even though most of those operations are actually implemented in the DRM core. Yeah, the core does the heavy lifting, but you still gotta show up and define the struct. πββοΈ
The resulting struct file_operations must be stored in the drm_driver.fops field.
The mandatory functions are drm_open, drm_read, drm_ioctl and drm_compat_ioctl if CONFIG_COMPAT is enabled. Note that drm_compat_ioctl will be NULL if CONFIG_COMPAT=n, so there's no need to sprinkle #ifdef into the code. slay, no preprocessor crimes today π§
Drivers which implement private ioctls that require 32/64 bit compatibility support must provide their own file_operations.compat_ioctl handler that processes private ioctls and calls drm_compat_ioctl for core ioctls.
In addition drm_read and drm_poll provide support for DRM events. DRM events are a generic and extensible means to send asynchronous events to userspace through the file descriptor. They are used to send vblank event and page flip completions by the KMS API. π« But drivers can also use it for their own needs, e.g. to signal completion of rendering.
For the driver-side event interface, see drm_event_reserve_init and drm_send_event as the main starting points. π«Ά
The memory mapping implementation will vary depending on how the driver manages memory. For GEM-based drivers this is drm_gem_mmap.
heads up: no other file operations are supported by the DRM userspace API. that's the whole list, don't get creative π
Overall the following is an example file_operations structure:
static const example_drm_fops = {
.owner = THIS_MODULE,
.open = drm_open,
.release = drm_release,
.unlocked_ioctl = drm_ioctl,
.compat_ioctl = drm_compat_ioctl, // NULL if CONFIG_COMPAT=n
.poll = drm_poll,
.read = drm_read,
.mmap = drm_gem_mmap,
};
For plain GEM based drivers there is the DEFINE_DRM_GEM_FOPS macro, and for DMA based drivers there is the DEFINE_DRM_GEM_DMA_FOPS macro to make this simpler. macros that save you typing are literally my love language β¨
The driver's file_operations must be stored in drm_driver.fops. (yeah, i said it twice, it's that important π
)
For driver-private IOCTL handling see the more detailed discussion in :ref:IOCTL support in the userland interfaces chapter<drm_driver_ioctl>`` .
β¨ SEE ALSO β¨
the whole friend group, no cap π:
- Kernel file
./drivers/gpu/drm/drm_file.c drm_file_alloc(9)drm_file_free(9)drm_open(9)drm_release(9)drm_release_noglobal(9)drm_read(9)drm_poll(9)drm_event_reserve_init_locked(9)drm_event_reserve_init(9)drm_event_cancel_free(9)drm_send_event_timestamp_locked(9)drm_send_event_locked(9)drm_send_event(9)drm_print_memory_stats(9)drm_show_memory_stats(9)drm_show_fdinfo(9)drm_file_err(9)mock_drm_getfile(9)
okay that's the whole page, go be iconic with your file_operations ππ this one lives over in ./drivers/gpu/drm, it's dated October 2026, and it came straight out of the Kernel API Manual. mwah, study safe ππ¦
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms