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.\" 27*68f7a013SJaakko Heinonen.Dd October 7, 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 134*68f7a013SJaakko Heinonen.Bl -tag -width "MAKEDEV_CHECKNAME" -compact -offset indent 135*68f7a013SJaakko Heinonen.It MAKEDEV_REF 136*68f7a013SJaakko Heinonenreference the created device 137*68f7a013SJaakko Heinonen.It MAKEDEV_NOWAIT 138*68f7a013SJaakko Heinonendo not sleep, may return NULL 139*68f7a013SJaakko Heinonen.It MAKEDEV_WAITOK 140*68f7a013SJaakko Heinonenallow the function to sleep to satisfy malloc 141*68f7a013SJaakko Heinonen.It MAKEDEV_ETERNAL 142*68f7a013SJaakko Heinonencreated device will be never destroyed 143*68f7a013SJaakko Heinonen.It MAKEDEV_CHECKNAME 144*68f7a013SJaakko Heinonenreturn NULL if the device name is invalid or already exists 145*68f7a013SJaakko Heinonen.El 146fe754649SKonstantin Belousov.Pp 147fe754649SKonstantin BelousovThe 1482b848cb1SKonstantin Belousov.Dv MAKEDEV_WAITOK 1492b848cb1SKonstantin Belousovflag is assumed if none of 1502b848cb1SKonstantin Belousov.Dv MAKEDEV_WAITOK , 1512b848cb1SKonstantin Belousov.Dv MAKEDEV_NOWAIT 1522b848cb1SKonstantin Belousovis specified. 1532b848cb1SKonstantin Belousov.Pp 1542b848cb1SKonstantin BelousovThe 155fe754649SKonstantin Belousov.Xr dev_clone 9 156fe754649SKonstantin Belousovevent handler shall specify 157fe754649SKonstantin Belousov.Dv MAKEDEV_REF 158fe754649SKonstantin Belousovflag when creating a device in response to lookup, to avoid race where 159fe754649SKonstantin Belousovthe device created is destroyed immediately after 160fe754649SKonstantin Belousov.Xr devfs_lookup 9 161fe754649SKonstantin Belousovdrops his reference to cdev. 162fe754649SKonstantin Belousov.Pp 163fe754649SKonstantin BelousovThe 1645e9cd1dbSKonstantin Belousov.Dv MAKEDEV_ETERNAL 1655e9cd1dbSKonstantin Belousovflag allows the kernel to not acquire some locks when translating system 1665e9cd1dbSKonstantin Belousovcalls into the cdevsw methods calls. 1675e9cd1dbSKonstantin BelousovIt is responsibility of the driver author to make sure that 1685e9cd1dbSKonstantin Belousov.Fn destroy_dev 1695e9cd1dbSKonstantin Belousovis never called on the returned cdev. 1705e9cd1dbSKonstantin BelousovFor the convenience, use the 1715e9cd1dbSKonstantin Belousov.Dv MAKEDEV_ETERNAL_KLD 1725e9cd1dbSKonstantin Belousovflag for the code that can be compiled into kernel or loaded 1735e9cd1dbSKonstantin Belousov(and unloaded) as loadable module. 1745e9cd1dbSKonstantin Belousov.Pp 175*68f7a013SJaakko HeinonenA panic will occur if the MAKEDEV_CHECKNAME flag is not specified 176*68f7a013SJaakko Heinonenand the device name is invalid or already exists. 177*68f7a013SJaakko Heinonen.Pp 1785e9cd1dbSKonstantin BelousovThe 179fe754649SKonstantin Belousov.Fn make_dev_cred 180fe754649SKonstantin Belousovfunction is equivalent to the call 181fe754649SKonstantin Belousov.Bd -literal -offset indent 182edde8745SEd Schoutenmake_dev_credf(0, cdevsw, unit, cr, uid, gid, perms, fmt, ...); 183e10c1be4SUlrich Spörlein.Ed 184fe754649SKonstantin Belousov.Pp 185fe754649SKonstantin BelousovThe 186fe754649SKonstantin Belousov.Fn make_dev 187fe754649SKonstantin Belousovfunction call is the same as 188fe754649SKonstantin Belousov.Bd -literal -offset indent 189edde8745SEd Schoutenmake_dev_credf(0, cdevsw, unit, NULL, uid, gid, perms, fmt, ...); 190e10c1be4SUlrich Spörlein.Ed 191fe754649SKonstantin Belousov.Pp 192fe754649SKonstantin BelousovThe 193792b2369SMatt Jacob.Fn make_dev_alias 194792b2369SMatt Jacobfunction takes the returned 195c97dc39fSChristian Brueffer.Ft cdev 196792b2369SMatt Jacobfrom 197792b2369SMatt Jacob.Fn make_dev 198edf0984dSRuslan Ermilovand makes another (aliased) name for this device. 199edf0984dSRuslan ErmilovIt is an error to call 200792b2369SMatt Jacob.Fn make_dev_alias 201792b2369SMatt Jacobprior to calling 202792b2369SMatt Jacob.Fn make_dev . 203792b2369SMatt Jacob.Pp 204792b2369SMatt JacobThe 2052557913dSJohn-Mark Gurney.Fa cdev 2062557913dSJohn-Mark Gurneyreturned by 2072557913dSJohn-Mark Gurney.Fn make_dev 2082557913dSJohn-Mark Gurneyand 2092557913dSJohn-Mark Gurney.Fn make_dev_alias 2102557913dSJohn-Mark Gurneyhas two fields, 2112557913dSJohn-Mark Gurney.Fa si_drv1 2122557913dSJohn-Mark Gurneyand 2132557913dSJohn-Mark Gurney.Fa si_drv2 , 21477213b87SChristian Bruefferthat are available to store state. 21577213b87SChristian BruefferBoth fields are of type 2162557913dSJohn-Mark Gurney.Ft void * . 2172557913dSJohn-Mark GurneyThese are designed to replace the 218edde8745SEd Schouten.Fa unit 2192557913dSJohn-Mark Gurneyargument to 22066c6ebfeSEd Schouten.Fn make_dev , 22166c6ebfeSEd Schoutenwhich can be obtained with 22266c6ebfeSEd Schouten.Fn dev2unit . 2232557913dSJohn-Mark Gurney.Pp 2242557913dSJohn-Mark GurneyThe 225bd00dbfaSBruce Evans.Fn destroy_dev 22694b1839dSChris Costellofunction takes the returned 227c97dc39fSChristian Brueffer.Fa cdev 22894b1839dSChris Costellofrom 22994b1839dSChris Costello.Fn make_dev 230edf0984dSRuslan Ermilovand destroys the registration for that device. 231fe754649SKonstantin BelousovThe notification is sent to 232fe754649SKonstantin Belousov.Xr devctl 4 233fe754649SKonstantin Belousovabout the destruction event. 234edf0984dSRuslan ErmilovDo not call 235792b2369SMatt Jacob.Fn destroy_dev 236792b2369SMatt Jacobon devices that were created with 237792b2369SMatt Jacob.Fn make_dev_alias . 238f14b89faSDima Dorfman.Pp 239f14b89faSDima DorfmanThe 24044c63b64SDima Dorfman.Fn dev_depends 241f14b89faSDima Dorfmanfunction establishes a parent-child relationship between two devices. 242f14b89faSDima DorfmanThe net effect is that a 243f14b89faSDima Dorfman.Fn destroy_dev 244f14b89faSDima Dorfmanof the parent device will also result in the destruction of the 245f14b89faSDima Dorfmanchild device(s), 246f14b89faSDima Dorfmanif any exist. 247f14b89faSDima DorfmanA device may simultaneously be a parent and a child, 248b82af3f5SMike Pritchardso it is possible to build a complete hierarchy. 249fe754649SKonstantin Belousov.Pp 250fe754649SKonstantin BelousovThe 251fe754649SKonstantin Belousov.Fn destroy_dev_sched_cb 252fe754649SKonstantin Belousovfunction schedules execution of the 253fe754649SKonstantin Belousov.Fn destroy_dev 254fe754649SKonstantin Belousovfor the specified 255fe754649SKonstantin Belousov.Fa cdev 256fe754649SKonstantin Belousovin the safe context. 257fe754649SKonstantin BelousovAfter 258fe754649SKonstantin Belousov.Fn destroy_dev 259fe754649SKonstantin Belousovis finished, and if the supplied 260fe754649SKonstantin Belousov.Fa cb 261fe754649SKonstantin Belousovis not NULL, the callback 262fe754649SKonstantin Belousov.Fa cb 263fe754649SKonstantin Belousovis called, with argument 264fe754649SKonstantin Belousov.Fa arg . 265fe754649SKonstantin BelousovThe 266fe754649SKonstantin Belousov.Fn destroy_dev_sched 267fe754649SKonstantin Belousovfunction is the same as 268fe754649SKonstantin Belousov.Bd -literal -offset indent 2693035649eSPawel Jakub Dawidekdestroy_dev_sched_cb(cdev, NULL, NULL); 270e10c1be4SUlrich Spörlein.Ed 271fe754649SKonstantin Belousov.Pp 272fe754649SKonstantin BelousovThe 273fe754649SKonstantin Belousov.Fn d_close 274fe754649SKonstantin Belousovdriver method cannot call 275fe754649SKonstantin Belousov.Fn destroy_dev 276fe754649SKonstantin Belousovdirectly. Doing so causes deadlock when 277fe754649SKonstantin Belousov.Fn destroy_dev 278fe754649SKonstantin Belousovwaits for all threads to leave the driver methods. 279fe754649SKonstantin BelousovAlso, because 280fe754649SKonstantin Belousov.Fn destroy_dev 281fe754649SKonstantin Belousovsleeps, no non-sleepable locks may be held over the call. 282fe754649SKonstantin BelousovThe 283fe754649SKonstantin Belousov.Fn destroy_dev_sched 284fe754649SKonstantin Belousovfamily of functions overcome these issues. 285fe754649SKonstantin Belousov.Pp 286fe754649SKonstantin BelousovThe device driver may call the 287fe754649SKonstantin Belousov.Fn destroy_dev_drain 288fe754649SKonstantin Belousovfunction to wait until all devices that have supplied 289fe754649SKonstantin Belousov.Fa csw 290fe754649SKonstantin Belousovas cdevsw, are destroyed. This is useful when driver knows that 291fe754649SKonstantin Belousov.Fn destroy_dev_sched 292fe754649SKonstantin Belousovis called for all instantiated devices, but need to postpone module 293fe754649SKonstantin Belousovunload until 294fe754649SKonstantin Belousov.Fn destroy_dev 295fe754649SKonstantin Belousovis actually finished for all of them. 296fe754649SKonstantin Belousov.Pp 297e07f1d5fSHiten Pandya.Sh SEE ALSO 298fe754649SKonstantin Belousov.Xr devctl 4 , 299fe754649SKonstantin Belousov.Xr destroy_dev_drain 9 , 300fe754649SKonstantin Belousov.Xr dev_clone 9 , 301e07f1d5fSHiten Pandya.Xr devfs 5 30294b1839dSChris Costello.Sh HISTORY 30394b1839dSChris CostelloThe 30494b1839dSChris Costello.Fn make_dev 30594b1839dSChris Costelloand 306bd00dbfaSBruce Evans.Fn destroy_dev 30794b1839dSChris Costellofunctions first appeared in 30894b1839dSChris Costello.Fx 4.0 . 309792b2369SMatt JacobThe function 310792b2369SMatt Jacob.Fn make_dev_alias 311792b2369SMatt Jacobfirst appeared in 312792b2369SMatt Jacob.Fx 4.1 . 313f14b89faSDima DorfmanThe function 31444c63b64SDima Dorfman.Fn dev_depends 315f14b89faSDima Dorfmanfirst appeared in 316f14b89faSDima Dorfman.Fx 5.0 . 317fe754649SKonstantin BelousovThe functions 318fe754649SKonstantin Belousov.Fn make_dev_credf , 319fe754649SKonstantin Belousov.Fn destroy_dev_sched , 320fe754649SKonstantin Belousov.Fn destroy_dev_sched_cb 321fe754649SKonstantin Belousovfirst appeared in 322fe754649SKonstantin Belousov.Fx 7.0 . 323