DRIVER_MODULE(9)

πŸ’– DRIVER_MODULE(9) πŸ’–

okay bestie, welcome to the DRIVER_MODULE(9) page from the Kernel Developer's Manual β€” yes we're in the kernel dev manual, we're that girl now. grab a boba, this one's about how kernel drivers announce themselves to the world ✨


🌸 NAME 🌸

DRIVER_MODULE, DRIVER_MODULE_ORDERED, EARLY_DRIVER_MODULE, EARLY_DRIVER_MODULE_ORDERED β€” the macro that declares a kernel driver, fr fr πŸ’…


✨ SYNOPSIS ✨

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>

DRIVER_MODULE(name, busname, driver_t driver, devclass_t devclass,
    modeventhand_t evh, void *arg);

DRIVER_MODULE_ORDERED(name, busname, driver_t driver,
    devclass_t devclass, modeventhand_t evh, void *arg, int order);

EARLY_DRIVER_MODULE(name, busname, driver_t driver, devclass_t devclass,
    modeventhand_t evh, void *arg, enum sysinit_elem_order order,
    int pass);

EARLY_DRIVER_MODULE_ORDERED(name, busname, driver_t driver,
    devclass_t devclass, modeventhand_t evh, void *arg,
    enum sysinit_elem_order order, int pass);

πŸ¦‹ DESCRIPTION πŸ¦‹

so like, DRIVER_MODULE() is the macro that declares a kernel driver. it expands into the actual real driver declaration, and that phrase name gets used as the naming prefix for the driver AND all its functions. lowkey important: it's supplied as plain text, not a char or a char *. no cap 🫢

busname is the parent bus of the driver β€” think PCI, ISA, PPBUS and the rest of the squad β€” like pci, isa, or ppbus. iconic lineup honestly.

the identifier you use in DRIVER_MODULE() can be different from the driver name πŸ‘€. also, the same driver identifier can totally live on different buses β€” which is a pretty clean way of making front ends for different cards using the same driver on the same or different buses. like, this is allowed, and it slays:

DRIVER_MODULE(foo, isa, foo_driver, foo_devclass, NULL, NULL);

DRIVER_MODULE(foo, pci, foo_driver, foo_devclass, NULL, NULL);

driver is the driver of type driver_t, which holds all the info about the driver β€” and that's why it's one of the two most important parts of the call to DRIVER_MODULE(). main character energy tbh πŸ‘‘

the devclass argument carries the kernel-internal info about the device, and it'll be used inside the kernel driver module. πŸ’–

the evh argument is the event handler β€” it gets called whenever the driver (or module) is loaded or unloaded (peep module(9) for the tea).

the arg is unused at this time and should be a NULL pointer. it's giving "invited but didn't show up" 😭


DRIVER_MODULE_ORDERED() lets a driver register in a specific order. super useful if one single kernel module contains multiple drivers that are inter-dependent (they're in their codependent era πŸ™ˆ). the order argument should be one of the SYSINIT(9) initialization ordering constants (SI_ORDER_*). the default order for a driver module is SI_ORDER_MIDDLE. typically a module will specify an order of SI_ORDER_ANY for a single driver to make sure it's registered last β€” closing the group chat, last one out πŸšͺ✨


EARLY_DRIVER_MODULE() lets a driver register for a specific pass level. here's the vibe: the boot time probe and attach process makes multiple passes over the device tree. certain critical drivers that provide basic services needed by other devices get attached during earlier passes β€” they're the ones who show up early and set up the venue πŸŽ€. most drivers are attached in a final general pass. a driver that attaches during an early pass must register for a specific pass level (BUS_PASS_*) via the pass argument. and once a driver is registered, it's available to attach to devices for all subsequent passes. periodt. πŸ’…


EARLY_DRIVER_MODULE_ORDERED() lets a driver register both in a specific order and for a specific pass level. best of both worlds, we love a multitasker 🌈


πŸ‘‘ SEE ALSO πŸ‘‘


πŸ’œ AUTHORS πŸ’œ

this manual page was written by Alexander Langer <alex@FreeBSD.org>. thank you for your service king 🫑


and that's a wrap bestie!! this page came from the <strong>Debian</strong> crew, dated <strong>February 12, 2018</strong>, and yes it's still filed under <strong>DRIVER_MODULE(9)</strong> in the kernel developer manual β€” go forth and declare ur drivers, ily πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from