DEV_MODULE(9)

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

πŸ’… 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