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