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, and arg is its argument. (want the full lore? go read DECLARE_MODULE(9), she explains the whole situation.)
  • arg β€” the thing that gets handed to evh, hence the void *, 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