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

# NAME

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

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

int
`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

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 driver(9).

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 `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`() and `devsw_detach`() return 0. Otherwise they return 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

driver(9)

April 30, 2017 | NetBSD 9.2