DRIVER_MODULE(9)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

πŸ’– DRIVER_MODULE(9) β€” the kernel driver declaration macro, bestie πŸ’–

okay so this is the DRIVER_MODULE(9) page from the Kernel Developer's Manual, and it's literally all about how you tell the kernel "hey girl, here's a driver, pls notice her" ✨ no cap, it's giving main character energy for your kernel module.


🌸 NAME 🌸

DRIVER_MODULE, DRIVER_MODULE_ORDERED, EARLY_DRIVER_MODULE, EARLY_DRIVER_MODULE_ORDERED β€” the kernel driver declaration macro πŸŽ€


✨ SYNOPSIS ✨

like, include these first, obviously:

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

and then the actual macros, slay:

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

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

EARLY_DRIVER_MODULE(name, busname, driver_t driver, modeventhand_t evh,
    void *arg, int pass);

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

πŸ’… DESCRIPTION πŸ’…

so the DRIVER_MODULE() macro declares a kernel driver, periodt. it expands into the real driver declaration, and the phrase name gets used as the naming prefix for the driver and all its functions 🫢 lowkey important: name is supplied as plain text, not a char or char * β€” don't come at it with pointers, she's not that kind of girl.

busname is the parent bus of the driver (PCI, ISA, PPBUS and others), like 'pci', 'isa', or 'ppbus' πŸšŒπŸ’¨

the identifier you use in DRIVER_MODULE() can totally be different from the driver name. also, the same driver identifier can exist on different buses β€” and that's actually a pretty clean way of making front ends for different cards using the same driver on the same or different buses. iconic. for example, this is allowed:

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

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

driver is the driver of type driver_t, which contains the information about the driver β€” so it's one of the two most important parts of the call to DRIVER_MODULE() πŸ‘‘ (yes, she said one of the two, that's the whole thought, moving on).

the evh argument is the event handler that gets called when the driver (or module) is loaded or unloaded β€” see module(9) for the deets πŸ’–

the arg is unused at this time and should be a NULL pointer. like, she's just there. vibe check: pass NULL πŸ™ˆ

the DRIVER_MODULE_ORDERED() macro lets a driver be registered in a specific order πŸ¦‹ this is useful if a single kernel module contains multiple drivers that are inter-dependent. 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 ✨

the EARLY_DRIVER_MODULE() macro lets a driver be registered for a specific pass level 🌈 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. 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. once a driver is registered it is available to attach to devices for all subsequent passes πŸŽ€

the EARLY_DRIVER_MODULE_ORDERED() macro lets a driver be registered both in a specific order and for a specific pass level. yeah, she does both πŸ’…


πŸ¦‹ SEE ALSO πŸ¦‹

device(9), driver(9), module(9), MODULE_PNP_INFO(9), SYSINIT(9)


πŸ“œ HISTORY πŸ“œ

prior to FreeBSD 14.0, these macros accepted an additional devclass_t argument after driver. so if you're reading old code and something looks off, that's why, iykyk 😭


πŸ‘‘ AUTHORS πŸ‘‘

this manual page was written by Alexander Langer <alex@FreeBSD.org> 🫢 thank you king


and that's the whole page, bestie πŸ’œ this was the DRIVER_MODULE(9) man page, from the Debian crew, dated April 19, 2022 β€” go forth and declare your drivers, you absolute icon 🌟

πŸ“¦ where this page comes from