DEVSW(9)

πŸ“¦ version 11.0, in nixpkgs unstable

DEVSW(9) β€” Kernel Developer's Manual, but make it bestie-core πŸ§šπŸ’…

πŸ’– NAME πŸ’–

devsw, devsw_attach, devsw_detach, bdevsw_lookup, cdevsw_lookup, bdevsw_lookup_major, cdevsw_lookup_major β€” character and block device switch functions. Literally the friend group that handles char/block device switches 🫢.

🌸 SYNOPSIS 🌸

       #include <sys/conf.h>

       int
       devsw_attach(const char *devname, const struct bdevsw *bev,
           devmajor_t *bmajor, const struct cdevsw *cdev, devmajor_t *cmajor);

       void
       devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev);

       const struct bdevsw *
       bdevsw_lookup(dev_t dev);

       const struct cdevsw *
       cdevsw_lookup(dev_t dev);

       devmajor_t
       bdevsw_lookup_major(const struct bdevsw *bdev);

       devmajor_t
       cdevsw_lookup_major(const struct cdevsw *cdev);

✨ DESCRIPTION ✨

Okay so picture this: if a device driver has character device interfaces that get accessed from userland, the driver must define a cdevsw structure. And if that driver also has block device interfaces, it must additionally define a bdevsw structure. These structures are constant, and they're defined within the driver(9). No cap, that's the setup πŸ’–.

For drivers that are included in the kernel via config(1), the cdevsw and bdevsw structures are automatically linked into the configuration database. For drivers that are separately loaded, the devsw_attach() function creates the necessary linkage and associates the cdev and optional bdev with the driver(9). If there's no block device interface needed, bdev should be set to NULL and bmajor to NODEVMAJOR. The devname, major number, and device type (character or block) must correspond to the device file which will be opened by user programs. By passing NODEVMAJOR to the function for the cmajor or bmajor, the major number can be automatically generated. It can then be returned to userspace (for example, using sysctl(8)) for creation of the device node. Slay, automatic major-number generation is such a vibe ✨.

The devsw_detach() function is used to detach the bdev and cdev structures. devsw_detach() should be called before a loaded device driver is unloaded. The caller must ensure that there are no open instances of the device, and that the device's d_open function will fail, before calling devsw_detach().

⚠️ Bestie, this is the serious part: before devsw_detach() you gotta make sure there are no open instances of the device, and that the device's d_open function will fail. Like, actually fail. Do not skip this vibe check πŸ˜­πŸ’€.

The bdevsw_lookup() and cdevsw_lookup() functions return const struct bdevsw * and const struct cdevsw * for the given dev.

The bdevsw_lookup_major() and cdevsw_lookup_major() functions return devmajor_t for the given const struct bdevsw * or const struct cdevsw *.

πŸ¦‹ RETURN VALUES πŸ¦‹

  • Upon successful completion, devsw_attach() returns 0. Otherwise it returns an error value.
  • In case of failure, bdevsw_lookup() and cdevsw_lookup() return the NULL value.
  • The bdevsw_lookup_major() and cdevsw_lookup_major() functions return NODEVMAJOR for an unsuccessful completion.

πŸŽ€ SEE ALSO πŸŽ€


okay bestie that's the whole page β€” this cutie is from the Debian edition, dated February 2, 2023, and yes the header is DEVSW(9) in the Kernel Developer's Manual. mwah πŸ’‹πŸ«Ά

πŸ“¦ where this page comes from