DEV_MODULE(9)
π DEV_MODULE(9) β bestie's guide to the kernel dev module macro π
okay so real talk β this is a page straight out of the Kernel Developer's Manual, and it's about DEV_MODULE, the macro that declares a device driver kernel module. yes it's kernel land. yes it's a vibe. let's go β¨
πΈ 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);
π DESCRIPTION π
so the DEV_MODULE() macro is literally the glow-up that declares a device driver kernel module. it fills in a moduledata_t structure and then calls DECLARE_MODULE() with the correct args β iconic behavior honestly π
breaking it down, name is the name of the module, and evh (with its argument arg) is the event handler for the module. (if you want the full tea on that, go read DECLARE_MODULE(9) β it's all there, iykyk π
)
and what is the event handler supposed to do? great question bestie: it's supposed to create the device with make_dev() on load, and destroy it when it's unloaded using destroy_dev(). that's the whole assignment. no notes. π
π¦ EXAMPLES π¦
here's the whole thing in action β copy-paste energy, but like, responsibly:
#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);
like, MOD_LOAD makes the device, MOD_UNLOAD and MOD_SHUTDOWN destroy it, and anything else gets EINVAL. slay. periodt. πβ¨
π SEE ALSO π
go read all of them, they're all main character energy fr fr ππ
π AUTHORS π
this manual page was written by Alexander Langer <alex@FreeBSD.org>. ty for your service king π«‘
πΈ okay that's the whole page bestie β this version came from Debian, dated January 19, 2012. go write some device drivers, you absolute icon π π¦β
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2001 Alexander Langer
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms