1.\" $NetBSD: devsw_attach.9,v 1.3 2017/04/30 12:30:00 pgoyette Exp $ 2.\" 3.\" Copyright (c) 2015 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Kamil Rytarowski. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd April 30, 2017 31.Dt DEVSW 9 32.Os 33.Sh NAME 34.Nm devsw , 35.Nm devsw_attach , 36.Nm devsw_detach , 37.Nm bdevsw_lookup , 38.Nm cdevsw_lookup , 39.Nm bdevsw_lookup_major , 40.Nm cdevsw_lookup_major 41.Nd character and block device switch functions 42.Sh SYNOPSIS 43.In sys/conf.h 44.Ft int 45.Fo devsw_attach 46.Fa "const char *devname" 47.Fa "const struct bdevsw *bev" 48.Fa "devmajor_t *bmajor" 49.Fa "const struct cdevsw *cdev" 50.Fa "devmajor_t *cmajor" 51.Fc 52.Ft int 53.Fo devsw_detach 54.Fa "const struct bdevsw *bdev" 55.Fa "const struct cdevsw *cdev" 56.Fc 57.Ft "const struct bdevsw *" 58.Fo bdevsw_lookup 59.Fa "dev_t dev" 60.Fc 61.Ft "const struct cdevsw *" 62.Fo cdevsw_lookup 63.Fa "dev_t dev" 64.Fc 65.Ft devmajor_t 66.Fo bdevsw_lookup_major 67.Fa "const struct bdevsw *bdev" 68.Fc 69.Ft devmajor_t 70.Fo cdevsw_lookup_major 71.Fa "const struct cdevsw *cdev" 72.Fc 73.Sh DESCRIPTION 74If a device driver has character device interfaces accessed from 75userland, the driver must define a 76.Em cdevsw 77structure. 78If the driver also has block device interfaces, the driver must 79additionally define a 80.Em bdevsw 81structure. 82These structures are constant, and are defined within the 83.Xr driver 9 . 84.Pp 85For drivers which are included in the kernel via 86.Xr config 1 , 87the 88.Em cdevsw 89and 90.Em bdevsw 91structures are automatically linked into the configuration database. 92For drivers which are separately loaded, the 93.Fn devsw_attach 94function creates the necessary linkage and associates the 95.Em cdev 96and optional 97.Em bdev 98with the 99.Xr driver 9 . 100If there is no block device interface needed, 101.Em bdev 102should be set to 103.Dv NULL 104and 105.Em bmajor 106to 107.Dv \-1 . 108The 109.Em devname , 110major number, and device type 111(character or block) 112must correspond to the device file which will be opened by user programs. 113By passing 114.Dv \-1 115to the function for the 116.Em cmajor 117or 118.Em bmajor , 119the major number can be automatically generated. 120It can then be returned to userspace (for example, using 121.Xr sysctl 8 ) 122for creation of the device node. 123.Pp 124The 125.Fn devsw_detach 126function is used to detach the 127.Em bdev 128and 129.Em cdev 130structures. 131.Fn devsw_detach 132should be called before a loaded device driver is unloaded. 133.Pp 134The 135.Fn bdevsw_lookup 136and 137.Fn cdevsw_lookup 138functions return 139.Em "const struct bdevsw *" 140and 141.Em "const struct cdevsw *" 142for the given 143.Em dev . 144.Pp 145The 146.Fn bdevsw_lookup_major 147and 148.Fn cdevsw_lookup_major 149functions return 150.Em "devmajor_t" 151for the given 152.Em "const struct bdevsw *" 153or 154.Em "const struct cdevsw *" . 155.Sh RETURN VALUES 156Upon successful completion, 157.Fn devsw_attach 158and 159.Fn devsw_detach 160return 0. 161Otherwise they return an error value. 162.Pp 163In case of failure, 164.Fn bdevsw_lookup 165and 166.Fn cdevsw_lookup 167return the 168.Dv NULL 169value. 170.Pp 171The 172.Fn bdevsw_lookup_major 173and 174.Fn cdevsw_lookup_major 175functions return 176.Dv NODEVMAJOR 177for an unsuccessful completion. 178.Sh SEE ALSO 179.Xr driver 9 180