DEV_MODULE(9)
π DEV_MODULE β device driver module declaration macro π
okay bestie, pull up a chair, we're in section 9 of the Kernel Developer's Manual and today's main character is a macro. yes, a macro. she's iconic, she's tiny, she does SO much. π
β¨ NAME β¨
DEV_MODULE β device driver module declaration macro π¦
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/conf.h>
DEV_MODULE(name, modeventhand_t evh, void *arg);
like, look at her. four includes, one macro. lowkey perfect. π
β¨ DESCRIPTION β¨
so what's the vibe here? The DEV_MODULE() macro declares a device driver kernel module. that's the whole job. she's a one-trick pony and she ATE. π
here's the tea on what she actually does: she fills in a moduledata_t structure for you (no cap, you don't have to touch it), and then she calls DECLARE_MODULE() with the correct args. that's it, that's the whole plot.
the pieces she takes:
nameβ the name of the module. it's givingβ¦ the module's name. πevhβ the event handler for the module, andargis its argument. (want the full lore? go readDECLARE_MODULE(9), she explains the whole situation.)argβ the thing that gets handed toevh, hence thevoid *, hence the mystery. π
and what's the event handler supposed to do, you ask? bestie. she's supposed to create the device with make_dev() when the thing loads, and to destroy it when it's unloaded using destroy_dev(). create on the way in, destroy on the way out. balance. harmony. the circle of life but for /dev nodes. πΈ
β¨ EXAMPLES β¨
okay here's the whole thing in action, character for character, do not touch:
#include <sys/module.h>
#include <sys/conf.h>
static struct cdevsw foo_devsw = { ... };
static struct cdev *sdev;
static int
foo_load(module_t mod, int cmd, void *arg)
{
int err = 0;
switch (cmd) {
case MOD_LOAD:
sdev = make_dev(&foo_devsw, 0, UID_ROOT, GID_WHEEL, 0600, "foo");
break; /* Success*/
case MOD_UNLOAD:
case MOD_SHUTDOWN:
destroy_dev(sdev);
break; /* Success*/
default:
err = EINVAL;
break;
}
return(err);
}
DEV_MODULE(foo, foo_load, NULL);
see how foo_load catches MOD_LOAD and makes the device, then catches MOD_UNLOAD and MOD_SHUTDOWN (yes both, she shares a case, efficient queen) and destroys it? and anything else she doesn't recognize? EINVAL. respectfully declined. π
and then the very last line, the payoff, the mic drop:
DEV_MODULE(foo, foo_load, NULL);
one line. she declared an entire driver module. periodt. π₯
β¨ SEE ALSO β¨
go read her friends, they're all slaying:
β¨ AUTHORS β¨
This manual page was written by Alexander Langer <alex@FreeBSD.org>. thank you for your service king π«Ά
Debian, January 19, 2012 β that's the version and the date, bestie, and that's a wrap on DEV_MODULE(9). go forth and declare your modules. you got this. mwah ππβ¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2001 Alexander Langer
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms