Updated: 2022/Sep/29

Please read Privacy Policy. It's for your privacy.

DEVSW(9)                   Kernel Developer's Manual                  DEVSW(9)

     devsw, devsw_attach, devsw_detach, bdevsw_lookup, cdevsw_lookup,
     bdevsw_lookup_major, cdevsw_lookup_major - character and block device
     switch functions

     #include <sys/conf.h>

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

     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);

     bdevsw_lookup_major(const struct bdevsw *bdev);

     cdevsw_lookup_major(const struct cdevsw *cdev);

     If a device driver has character device interfaces accessed from
     userland, the driver must define a cdevsw structure.  If the driver also
     has block device interfaces, the driver must additionally define a bdevsw
     structure.  These structures are constant, and are defined within the

     For drivers which are included in the kernel via config(1), the cdevsw
     and bdevsw structures are automatically linked into the configuration
     database.  For drivers which are separately loaded, the devsw_attach()
     function creates the necessary linkage and associates the cdev and
     optional bdev with the driver(9).  If there is no block device interface
     needed, bdev should be set to NULL and bmajor to -1.  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 -1 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.

     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.  Fn devsw_detach .

     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 *.

     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

     The bdevsw_lookup_major() and cdevsw_lookup_major() functions return
     NODEVMAJOR for an unsuccessful completion.


NetBSD 9.99                    January 11, 2022                    NetBSD 9.99