xref: /netbsd-src/share/man/man9/devsw_attach.9 (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1.\"	$NetBSD: devsw_attach.9,v 1.2 2015/05/14 13:59:15 wiz 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 May 13, 2015
31.Dt DEVSW_ATTACH 9
32.Os
33.Sh NAME
34.Nm devsw_attach ,
35.Nm devsw_detach ,
36.Nm bdevsw_lookup ,
37.Nm cdevsw_lookup ,
38.Nm bdevsw_lookup_major ,
39.Nm cdevsw_lookup_major
40.Nd character and block device switch functions
41.Sh SYNOPSIS
42.In sys/conf.h
43.Ft int
44.Fo devsw_attach
45.Fa "const char *devname"
46.Fa "const struct bdevsw *bev"
47.Fa "devmajor_t *bmajor"
48.Fa "const struct cdevsw *cdev"
49.Fa "devmajor_t *cmajor"
50.Fc
51.Ft int
52.Fo devsw_detach
53.Fa "const struct bdevsw *bdev"
54.Fa "const struct cdevsw *cdev"
55.Fc
56.Ft "const struct bdevsw *"
57.Fo bdevsw_lookup
58.Fa "dev_t dev"
59.Fc
60.Ft "const struct cdevsw *"
61.Fo cdevsw_lookup
62.Fa "dev_t dev"
63.Fc
64.Ft devmajor_t
65.Fo bdevsw_lookup_major
66.Fa "const struct bdevsw *bdev"
67.Fc
68.Ft devmajor_t
69.Fo cdevsw_lookup_major
70.Fa "const struct cdevsw *cdev"
71.Fc
72.Sh DESCRIPTION
73If a device driver has character device interfaces accessed from
74userland, the driver must define a
75.Em cdevsw
76structure.
77If the driver also has block device interfaces, the driver must
78additionally define a
79.Em bdevsw
80structure.
81These structures are constant, and are defined within the
82.Xr driver 9 .
83.Pp
84For drivers which are included in the kernel via
85.Xr config 1 ,
86the
87.Em cdevsw
88and
89.Em bdevsw
90structures are automatically linked into the configuration database.
91For drivers which are separately loaded, the
92.Fn devsw_attach
93function creates the necessary linkage and associates the
94.Em cdev
95and optional
96.Em bdev
97with the
98.Xr driver 9 .
99If there is no block device interface needed,
100.Em bdev
101should be set to
102.Dv NULL
103and
104.Em bmajor
105to
106.Dv \-1 .
107The
108.Em devname ,
109major number, and device type
110(character or block)
111must correspond to the device file which will be opened by user programs.
112By passing
113.Dv \-1
114to the function for the
115.Em cmajor
116or
117.Em bmajor ,
118the major number can be automatically generated.
119It can then be returned to userspace (for example, using
120.Xr sysctl 8 )
121for creation of the device node.
122.Pp
123The
124.Fn devsw_detach
125function is used to detach the
126.Em bdev
127and
128.Em cdev
129structures.
130.Fn devsw_detach
131should be called before a loaded device driver is unloaded.
132.Pp
133The
134.Fn bdevsw_lookup
135and
136.Fn cdevsw_lookup
137functions return
138.Em "const struct bdevsw *"
139and
140.Em "const struct cdevsw *"
141for the given
142.Em dev .
143.Pp
144The
145.Fn bdevsw_lookup_major
146and
147.Fn cdevsw_lookup_major
148functions return
149.Em "devmajor_t"
150for the given
151.Em "const struct bdevsw *"
152or
153.Em "const struct cdevsw *" .
154.Sh RETURN VALUES
155Upon successful completion,
156.Fn devsw_attach
157and
158.Fn devsw_detach
159return 0.
160Otherwise they return an error value.
161.Pp
162In case of failure,
163.Fn bdevsw_lookup
164and
165.Fn cdevsw_lookup
166return the
167.Dv NULL
168value.
169.Pp
170The
171.Fn bdevsw_lookup_major
172and
173.Fn cdevsw_lookup_major
174functions return
175.Dv NODEVMAJOR
176for an unsuccessful completion.
177.Sh SEE ALSO
178.Xr driver 9
179