xref: /netbsd-src/share/man/man9/devsw_attach.9 (revision 2718af68c3efc72c9769069b5c7f9ed36f6b9def)
1.\"	$NetBSD: devsw_attach.9,v 1.4 2022/03/28 12:33:20 riastradh 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 January 11, 2022
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 void
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.
133The caller must ensure that there are no open instances of the device,
134and that the device's
135.Fn d_open
136function will fail, before calling.
137Fn devsw_detach .
138.Pp
139The
140.Fn bdevsw_lookup
141and
142.Fn cdevsw_lookup
143functions return
144.Em "const struct bdevsw *"
145and
146.Em "const struct cdevsw *"
147for the given
148.Em dev .
149.Pp
150The
151.Fn bdevsw_lookup_major
152and
153.Fn cdevsw_lookup_major
154functions return
155.Em "devmajor_t"
156for the given
157.Em "const struct bdevsw *"
158or
159.Em "const struct cdevsw *" .
160.Sh RETURN VALUES
161Upon successful completion,
162.Fn devsw_attach
163returns 0.
164Otherwise it returns an error value.
165.Pp
166In case of failure,
167.Fn bdevsw_lookup
168and
169.Fn cdevsw_lookup
170return the
171.Dv NULL
172value.
173.Pp
174The
175.Fn bdevsw_lookup_major
176and
177.Fn cdevsw_lookup_major
178functions return
179.Dv NODEVMAJOR
180for an unsuccessful completion.
181.Sh SEE ALSO
182.Xr driver 9
183