xref: /freebsd/share/man/man9/make_dev.9 (revision e10c1be4504da72ac414836477d1baa8174a591c)
194b1839dSChris Costello.\" Copyright (c) 1999 Chris Costello
294b1839dSChris Costello.\" All rights reserved.
394b1839dSChris Costello.\"
494b1839dSChris Costello.\" Redistribution and use in source and binary forms, with or without
594b1839dSChris Costello.\" modification, are permitted provided that the following conditions
694b1839dSChris Costello.\" are met:
794b1839dSChris Costello.\" 1. Redistributions of source code must retain the above copyright
894b1839dSChris Costello.\"    notice, this list of conditions and the following disclaimer.
994b1839dSChris Costello.\" 2. Redistributions in binary form must reproduce the above copyright
1094b1839dSChris Costello.\"    notice, this list of conditions and the following disclaimer in the
1194b1839dSChris Costello.\"    documentation and/or other materials provided with the distribution.
1294b1839dSChris Costello.\"
1394b1839dSChris Costello.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1494b1839dSChris Costello.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1594b1839dSChris Costello.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1694b1839dSChris Costello.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1794b1839dSChris Costello.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1894b1839dSChris Costello.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1994b1839dSChris Costello.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2094b1839dSChris Costello.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2194b1839dSChris Costello.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2294b1839dSChris Costello.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2394b1839dSChris Costello.\" SUCH DAMAGE.
2494b1839dSChris Costello.\"
2594b1839dSChris Costello.\" $FreeBSD$
2694b1839dSChris Costello.\"
272b848cb1SKonstantin Belousov.Dd May 6, 2010
2894b1839dSChris Costello.Dt MAKE_DEV 9
29aa12cea2SUlrich Spörlein.Os
3094b1839dSChris Costello.Sh NAME
3194b1839dSChris Costello.Nm make_dev ,
32fe754649SKonstantin Belousov.Nm make_dev_cred ,
33fe754649SKonstantin Belousov.Nm make_dev_credf ,
34f14b89faSDima Dorfman.Nm make_dev_alias ,
35f14b89faSDima Dorfman.Nm destroy_dev ,
36fe754649SKonstantin Belousov.Nm destroy_dev_sched ,
37fe754649SKonstantin Belousov.Nm destroy_dev_sched_cb ,
38fe754649SKonstantin Belousov.Nm destroy_dev_drain ,
3944c63b64SDima Dorfman.Nm dev_depends
40e3718e31SRuslan Ermilov.Nd manage
41c97dc39fSChristian Brueffer.Vt cdev Ns 's
42e3718e31SRuslan Ermilovand DEVFS registration for devices
4394b1839dSChris Costello.Sh SYNOPSIS
44212c98aaSBruce Evans.In sys/param.h
4532eef9aeSRuslan Ermilov.In sys/conf.h
46097fcfebSWarner Losh.Ft struct cdev *
47edde8745SEd Schouten.Fn make_dev "struct cdevsw *cdevsw" "int unit" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" ...
48097fcfebSWarner Losh.Ft struct cdev *
49edde8745SEd Schouten.Fn make_dev_cred "struct cdevsw *cdevsw" "int unit" "struct ucred *cr" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" ...
50fe754649SKonstantin Belousov.Ft struct cdev *
51edde8745SEd Schouten.Fn make_dev_credf "int flags" "struct cdevsw *cdevsw" "int unit" "struct ucred *cr" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" ...
52fe754649SKonstantin Belousov.Ft struct cdev *
5374c9ea0cSJohn-Mark Gurney.Fn make_dev_alias "struct cdev *pdev" "const char *fmt" ...
5494b1839dSChris Costello.Ft void
55097fcfebSWarner Losh.Fn destroy_dev "struct cdev *dev"
56f14b89faSDima Dorfman.Ft void
57fe754649SKonstantin Belousov.Fn destroy_dev_sched "struct cdev *dev"
58fe754649SKonstantin Belousov.Ft void
59fe754649SKonstantin Belousov.Fn destroy_dev_sched_cb "struct cdev *dev" "void (*cb)(void *)" "void *arg"
60fe754649SKonstantin Belousov.Ft void
61fe754649SKonstantin Belousov.Fn destroy_dev_drain "struct cdevsw *csw"
62fe754649SKonstantin Belousov.Ft void
63097fcfebSWarner Losh.Fn dev_depends "struct cdev *pdev" "struct cdev *cdev"
6494b1839dSChris Costello.Sh DESCRIPTION
6594b1839dSChris CostelloThe
66fe754649SKonstantin Belousov.Fn make_dev_credf
6794b1839dSChris Costellofunction creates a
68c97dc39fSChristian Brueffer.Fa cdev
69a0942a60SHiten Pandyastructure for a new device.
70fe754649SKonstantin BelousovIt also notifies
71fe754649SKonstantin Belousov.Xr devfs 5
72fe754649SKonstantin Belousovof the presence of the new device, that causes corresponding nodes
73fe754649SKonstantin Belousovto be created.
74fe754649SKonstantin BelousovBesides this, a
75fe754649SKonstantin Belousov.Xr devctl 4
76fe754649SKonstantin Belousovnotification is sent.
77a0942a60SHiten PandyaThe device will be owned by
7894b1839dSChris Costello.Va uid ,
7994b1839dSChris Costellowith the group ownership as
801dfbd9c2SWarner Losh.Va gid .
811dfbd9c2SWarner LoshThe name is the expansion of
821dfbd9c2SWarner Losh.Va fmt
831dfbd9c2SWarner Loshand following arguments as
841dfbd9c2SWarner Losh.Xr printf 9
851dfbd9c2SWarner Loshwould print it.
861dfbd9c2SWarner LoshThe name determines its path under
871dfbd9c2SWarner Losh.Pa /dev
881dfbd9c2SWarner Loshor other
891dfbd9c2SWarner Losh.Xr devfs 5
901dfbd9c2SWarner Loshmount point and may contain slash
911dfbd9c2SWarner Losh.Ql /
921dfbd9c2SWarner Loshcharacters to denote subdirectories.
9394b1839dSChris CostelloThe permissions of the file specified in
9494b1839dSChris Costello.Va perms
9594b1839dSChris Costelloare defined in
96fe08efe6SRuslan Ermilov.In sys/stat.h :
9794b1839dSChris Costello.Pp
9894b1839dSChris Costello.Bd -literal -offset indent -compact
9994b1839dSChris Costello#define S_IRWXU 0000700    /* RWX mask for owner */
10094b1839dSChris Costello#define S_IRUSR 0000400    /* R for owner */
10194b1839dSChris Costello#define S_IWUSR 0000200    /* W for owner */
10294b1839dSChris Costello#define S_IXUSR 0000100    /* X for owner */
10394b1839dSChris Costello
10494b1839dSChris Costello#define S_IRWXG 0000070    /* RWX mask for group */
10594b1839dSChris Costello#define S_IRGRP 0000040    /* R for group */
10694b1839dSChris Costello#define S_IWGRP 0000020    /* W for group */
10794b1839dSChris Costello#define S_IXGRP 0000010    /* X for group */
10894b1839dSChris Costello
10994b1839dSChris Costello#define S_IRWXO 0000007    /* RWX mask for other */
11094b1839dSChris Costello#define S_IROTH 0000004    /* R for other */
11194b1839dSChris Costello#define S_IWOTH 0000002    /* W for other */
11294b1839dSChris Costello#define S_IXOTH 0000001    /* X for other */
11394b1839dSChris Costello
11494b1839dSChris Costello#define S_ISUID 0004000    /* set user id on execution */
11594b1839dSChris Costello#define S_ISGID 0002000    /* set group id on execution */
11694b1839dSChris Costello#define S_ISVTX 0001000    /* sticky bit */
11794b1839dSChris Costello#ifndef _POSIX_SOURCE
11894b1839dSChris Costello#define S_ISTXT 0001000
11994b1839dSChris Costello#endif
12094b1839dSChris Costello.Ed
12194b1839dSChris Costello.Pp
12294b1839dSChris CostelloThe
123fe754649SKonstantin Belousov.Va cr
124fe754649SKonstantin Belousovargument specifies credentials that will be stored in the
125fe754649SKonstantin Belousov.Fa si_cred
126fe754649SKonstantin Belousovmember of the initialized
127fe754649SKonstantin Belousov.Fa struct cdev .
128fe754649SKonstantin BelousovThe
129fe754649SKonstantin Belousov.Va flags
130fe754649SKonstantin Belousovargument alters the operation of
131fe754649SKonstantin Belousov.Fn make_dev_credf .
132fe754649SKonstantin BelousovThe following values are currently accepted:
133fe754649SKonstantin Belousov.Pp
134fe754649SKonstantin Belousov.Bd -literal -offset indent -compact
135fe754649SKonstantin BelousovMAKEDEV_REF	reference the created device
1362b848cb1SKonstantin BelousovMAKEDEV_NOWAIT	do not sleep, may return NULL
1372b848cb1SKonstantin BelousovMAKEDEV_WAITOK	allow the function to sleep to satisfy malloc
138fe754649SKonstantin Belousov.Ed
139fe754649SKonstantin Belousov.Pp
140fe754649SKonstantin BelousovThe
1412b848cb1SKonstantin Belousov.Dv MAKEDEV_WAITOK
1422b848cb1SKonstantin Belousovflag is assumed if none of
1432b848cb1SKonstantin Belousov.Dv MAKEDEV_WAITOK ,
1442b848cb1SKonstantin Belousov.Dv MAKEDEV_NOWAIT
1452b848cb1SKonstantin Belousovis specified.
1462b848cb1SKonstantin Belousov.Pp
1472b848cb1SKonstantin BelousovThe
148fe754649SKonstantin Belousov.Xr dev_clone 9
149fe754649SKonstantin Belousovevent handler shall specify
150fe754649SKonstantin Belousov.Dv MAKEDEV_REF
151fe754649SKonstantin Belousovflag when creating a device in response to lookup, to avoid race where
152fe754649SKonstantin Belousovthe device created is destroyed immediately after
153fe754649SKonstantin Belousov.Xr devfs_lookup 9
154fe754649SKonstantin Belousovdrops his reference to cdev.
155fe754649SKonstantin Belousov.Pp
156fe754649SKonstantin BelousovThe
157fe754649SKonstantin Belousov.Fn make_dev_cred
158fe754649SKonstantin Belousovfunction is equivalent to the call
159fe754649SKonstantin Belousov.Bd -literal -offset indent
160edde8745SEd Schoutenmake_dev_credf(0, cdevsw, unit, cr, uid, gid, perms, fmt, ...);
161*e10c1be4SUlrich Spörlein.Ed
162fe754649SKonstantin Belousov.Pp
163fe754649SKonstantin BelousovThe
164fe754649SKonstantin Belousov.Fn make_dev
165fe754649SKonstantin Belousovfunction call is the same as
166fe754649SKonstantin Belousov.Bd -literal -offset indent
167edde8745SEd Schoutenmake_dev_credf(0, cdevsw, unit, NULL, uid, gid, perms, fmt, ...);
168*e10c1be4SUlrich Spörlein.Ed
169fe754649SKonstantin Belousov.Pp
170fe754649SKonstantin BelousovThe
171792b2369SMatt Jacob.Fn make_dev_alias
172792b2369SMatt Jacobfunction takes the returned
173c97dc39fSChristian Brueffer.Ft cdev
174792b2369SMatt Jacobfrom
175792b2369SMatt Jacob.Fn make_dev
176edf0984dSRuslan Ermilovand makes another (aliased) name for this device.
177edf0984dSRuslan ErmilovIt is an error to call
178792b2369SMatt Jacob.Fn make_dev_alias
179792b2369SMatt Jacobprior to calling
180792b2369SMatt Jacob.Fn make_dev .
181792b2369SMatt Jacob.Pp
182792b2369SMatt JacobThe
1832557913dSJohn-Mark Gurney.Fa cdev
1842557913dSJohn-Mark Gurneyreturned by
1852557913dSJohn-Mark Gurney.Fn make_dev
1862557913dSJohn-Mark Gurneyand
1872557913dSJohn-Mark Gurney.Fn make_dev_alias
1882557913dSJohn-Mark Gurneyhas two fields,
1892557913dSJohn-Mark Gurney.Fa si_drv1
1902557913dSJohn-Mark Gurneyand
1912557913dSJohn-Mark Gurney.Fa si_drv2 ,
19277213b87SChristian Bruefferthat are available to store state.
19377213b87SChristian BruefferBoth fields are of type
1942557913dSJohn-Mark Gurney.Ft void * .
1952557913dSJohn-Mark GurneyThese are designed to replace the
196edde8745SEd Schouten.Fa unit
1972557913dSJohn-Mark Gurneyargument to
19866c6ebfeSEd Schouten.Fn make_dev ,
19966c6ebfeSEd Schoutenwhich can be obtained with
20066c6ebfeSEd Schouten.Fn dev2unit .
2012557913dSJohn-Mark Gurney.Pp
2022557913dSJohn-Mark GurneyThe
203bd00dbfaSBruce Evans.Fn destroy_dev
20494b1839dSChris Costellofunction takes the returned
205c97dc39fSChristian Brueffer.Fa cdev
20694b1839dSChris Costellofrom
20794b1839dSChris Costello.Fn make_dev
208edf0984dSRuslan Ermilovand destroys the registration for that device.
209fe754649SKonstantin BelousovThe notification is sent to
210fe754649SKonstantin Belousov.Xr devctl 4
211fe754649SKonstantin Belousovabout the destruction event.
212edf0984dSRuslan ErmilovDo not call
213792b2369SMatt Jacob.Fn destroy_dev
214792b2369SMatt Jacobon devices that were created with
215792b2369SMatt Jacob.Fn make_dev_alias .
216f14b89faSDima Dorfman.Pp
217f14b89faSDima DorfmanThe
21844c63b64SDima Dorfman.Fn dev_depends
219f14b89faSDima Dorfmanfunction establishes a parent-child relationship between two devices.
220f14b89faSDima DorfmanThe net effect is that a
221f14b89faSDima Dorfman.Fn destroy_dev
222f14b89faSDima Dorfmanof the parent device will also result in the destruction of the
223f14b89faSDima Dorfmanchild device(s),
224f14b89faSDima Dorfmanif any exist.
225f14b89faSDima DorfmanA device may simultaneously be a parent and a child,
226b82af3f5SMike Pritchardso it is possible to build a complete hierarchy.
227fe754649SKonstantin Belousov.Pp
228fe754649SKonstantin BelousovThe
229fe754649SKonstantin Belousov.Fn destroy_dev_sched_cb
230fe754649SKonstantin Belousovfunction schedules execution of the
231fe754649SKonstantin Belousov.Fn destroy_dev
232fe754649SKonstantin Belousovfor the specified
233fe754649SKonstantin Belousov.Fa cdev
234fe754649SKonstantin Belousovin the safe context.
235fe754649SKonstantin BelousovAfter
236fe754649SKonstantin Belousov.Fn destroy_dev
237fe754649SKonstantin Belousovis finished, and if the supplied
238fe754649SKonstantin Belousov.Fa cb
239fe754649SKonstantin Belousovis not NULL, the callback
240fe754649SKonstantin Belousov.Fa cb
241fe754649SKonstantin Belousovis called, with argument
242fe754649SKonstantin Belousov.Fa arg .
243fe754649SKonstantin BelousovThe
244fe754649SKonstantin Belousov.Fn destroy_dev_sched
245fe754649SKonstantin Belousovfunction is the same as
246fe754649SKonstantin Belousov.Bd -literal -offset indent
247fe754649SKonstantin Belousovdestroy_dev_sched(cdev, NULL, NULL);
248*e10c1be4SUlrich Spörlein.Ed
249fe754649SKonstantin Belousov.Pp
250fe754649SKonstantin BelousovThe
251fe754649SKonstantin Belousov.Fn d_close
252fe754649SKonstantin Belousovdriver method cannot call
253fe754649SKonstantin Belousov.Fn destroy_dev
254fe754649SKonstantin Belousovdirectly. Doing so causes deadlock when
255fe754649SKonstantin Belousov.Fn destroy_dev
256fe754649SKonstantin Belousovwaits for all threads to leave the driver methods.
257fe754649SKonstantin BelousovAlso, because
258fe754649SKonstantin Belousov.Fn destroy_dev
259fe754649SKonstantin Belousovsleeps, no non-sleepable locks may be held over the call.
260fe754649SKonstantin BelousovThe
261fe754649SKonstantin Belousov.Fn destroy_dev_sched
262fe754649SKonstantin Belousovfamily of functions overcome these issues.
263fe754649SKonstantin Belousov.Pp
264fe754649SKonstantin BelousovThe device driver may call the
265fe754649SKonstantin Belousov.Fn destroy_dev_drain
266fe754649SKonstantin Belousovfunction to wait until all devices that have supplied
267fe754649SKonstantin Belousov.Fa csw
268fe754649SKonstantin Belousovas cdevsw, are destroyed. This is useful when driver knows that
269fe754649SKonstantin Belousov.Fn destroy_dev_sched
270fe754649SKonstantin Belousovis called for all instantiated devices, but need to postpone module
271fe754649SKonstantin Belousovunload until
272fe754649SKonstantin Belousov.Fn destroy_dev
273fe754649SKonstantin Belousovis actually finished for all of them.
274fe754649SKonstantin Belousov.Pp
275e07f1d5fSHiten Pandya.Sh SEE ALSO
276fe754649SKonstantin Belousov.Xr devctl 4 ,
277fe754649SKonstantin Belousov.Xr destroy_dev_drain 9 ,
278fe754649SKonstantin Belousov.Xr dev_clone 9 ,
279e07f1d5fSHiten Pandya.Xr devfs 5
28094b1839dSChris Costello.Sh HISTORY
28194b1839dSChris CostelloThe
28294b1839dSChris Costello.Fn make_dev
28394b1839dSChris Costelloand
284bd00dbfaSBruce Evans.Fn destroy_dev
28594b1839dSChris Costellofunctions first appeared in
28694b1839dSChris Costello.Fx 4.0 .
287792b2369SMatt JacobThe function
288792b2369SMatt Jacob.Fn make_dev_alias
289792b2369SMatt Jacobfirst appeared in
290792b2369SMatt Jacob.Fx 4.1 .
291f14b89faSDima DorfmanThe function
29244c63b64SDima Dorfman.Fn dev_depends
293f14b89faSDima Dorfmanfirst appeared in
294f14b89faSDima Dorfman.Fx 5.0 .
295fe754649SKonstantin BelousovThe functions
296fe754649SKonstantin Belousov.Fn make_dev_credf ,
297fe754649SKonstantin Belousov.Fn destroy_dev_sched ,
298fe754649SKonstantin Belousov.Fn destroy_dev_sched_cb
299fe754649SKonstantin Belousovfirst appeared in
300fe754649SKonstantin Belousov.Fx 7.0 .
301