xref: /netbsd-src/share/man/man9/devsw_attach.9 (revision d1ec716407619686f25c938294c0b9407612d117)
1.\"	$NetBSD: devsw_attach.9,v 1.6 2023/02/02 14:09:52 uwe 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 February 2, 2023
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.Vt cdevsw
77structure.
78If the driver also has block device interfaces, the driver must
79additionally define a
80.Vt 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.Vt cdevsw
89and
90.Vt 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.Fa cdev
96and optional
97.Fa bdev
98with the
99.Xr driver 9 .
100If there is no block device interface needed,
101.Fa bdev
102should be set to
103.Dv NULL
104and
105.Fa bmajor
106to
107.Dv NODEVMAJOR .
108The
109.Fa 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 NODEVMAJOR
115to the function for the
116.Fa cmajor
117or
118.Fa bmajor ,
119the major number can be automatically generated.
120It can then be returned to userspace
121.Po
122for example, using
123.Xr sysctl 8
124.Pc
125for creation of the device node.
126.Pp
127The
128.Fn devsw_detach
129function is used to detach the
130.Fa bdev
131and
132.Fa cdev
133structures.
134.Fn devsw_detach
135should be called before a loaded device driver is unloaded.
136The caller must ensure that there are no open instances of the device,
137and that the device's
138.Fa d_open
139function will fail, before calling
140.Fn devsw_detach .
141.Pp
142The
143.Fn bdevsw_lookup
144and
145.Fn cdevsw_lookup
146functions return
147.Vt "const struct bdevsw *"
148and
149.Vt "const struct cdevsw *"
150for the given
151.Fa dev .
152.Pp
153The
154.Fn bdevsw_lookup_major
155and
156.Fn cdevsw_lookup_major
157functions return
158.Vt "devmajor_t"
159for the given
160.Vt "const struct bdevsw *"
161or
162.Vt "const struct cdevsw *" .
163.Sh RETURN VALUES
164Upon successful completion,
165.Fn devsw_attach
166returns 0.
167Otherwise it returns an error value.
168.Pp
169In case of failure,
170.Fn bdevsw_lookup
171and
172.Fn cdevsw_lookup
173return the
174.Dv NULL
175value.
176.Pp
177The
178.Fn bdevsw_lookup_major
179and
180.Fn cdevsw_lookup_major
181functions return
182.Dv NODEVMAJOR
183for an unsuccessful completion.
184.Sh SEE ALSO
185.Xr driver 9
186