DEVSW(9)
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: beforedevsw_detach()you gotta make sure there are no open instances of the device, and that the device'sd_openfunction 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()returns0. Otherwise it returns an error value. - In case of failure,
bdevsw_lookup()andcdevsw_lookup()return theNULLvalue. - The
bdevsw_lookup_major()andcdevsw_lookup_major()functions returnNODEVMAJORfor 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
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2015 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms