1/*- 2 * Copyright (c) 1999-2002 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 *
| 1/*- 2 * Copyright (c) 1999-2002 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 *
|
112}; 113 114 115struct cdevsw * 116devsw(dev_t dev) 117{ 118 if (dev->si_devsw) 119 return (dev->si_devsw); 120 return (&dead_cdevsw); 121} 122 123/* 124 * dev_t and u_dev_t primitives 125 */ 126 127int 128major(dev_t x) 129{ 130 if (x == NODEV) 131 return NOUDEV; 132 return((x->si_udev >> 8) & 0xff); 133} 134 135int 136minor(dev_t x) 137{ 138 if (x == NODEV) 139 return NOUDEV; 140 return(x->si_udev & 0xffff00ff); 141} 142 143int 144dev2unit(dev_t x) 145{ 146 int i; 147 148 if (x == NODEV) 149 return NOUDEV; 150 i = minor(x); 151 return ((i & 0xff) | (i >> 8)); 152} 153 154int 155unit2minor(int unit) 156{ 157 158 KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit)); 159 return ((unit & 0xff) | ((unit << 8) & ~0xffff)); 160} 161 162static dev_t 163allocdev(void) 164{ 165 static int stashed; 166 struct cdev *si; 167 168 if (LIST_FIRST(&dev_free)) { 169 si = LIST_FIRST(&dev_free); 170 LIST_REMOVE(si, si_hash); 171 } else if (stashed >= DEVT_STASH) { 172 MALLOC(si, struct cdev *, sizeof(*si), M_DEVT, 173 M_USE_RESERVE | M_ZERO | M_WAITOK); 174 } else { 175 si = devt_stash + stashed++; 176 bzero(si, sizeof *si); 177 si->si_flags |= SI_STASHED; 178 } 179 si->__si_namebuf[0] = '\0'; 180 si->si_name = si->__si_namebuf; 181 LIST_INIT(&si->si_children); 182 TAILQ_INIT(&si->si_snapshots); 183 return (si); 184} 185 186dev_t 187makedev(int x, int y) 188{ 189 struct cdev *si; 190 udev_t udev; 191 int hash; 192 193 if (x == umajor(NOUDEV) && y == uminor(NOUDEV)) 194 panic("makedev of NOUDEV"); 195 udev = (x << 8) | y; 196 hash = udev % DEVT_HASH; 197 LIST_FOREACH(si, &dev_hash[hash], si_hash) { 198 if (si->si_udev == udev) 199 return (si); 200 } 201 si = allocdev(); 202 si->si_udev = udev; 203 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); 204 return (si); 205} 206 207void 208freedev(dev_t dev) 209{ 210 211 if (!free_devt) 212 return; 213 if (SLIST_FIRST(&dev->si_hlist)) 214 return; 215 if (dev->si_devsw || dev->si_drv1 || dev->si_drv2) 216 return; 217 LIST_REMOVE(dev, si_hash); 218 if (dev->si_flags & SI_STASHED) { 219 bzero(dev, sizeof(*dev)); 220 dev->si_flags |= SI_STASHED; 221 LIST_INSERT_HEAD(&dev_free, dev, si_hash); 222 } else { 223 FREE(dev, M_DEVT); 224 } 225} 226 227udev_t 228dev2udev(dev_t x) 229{ 230 if (x == NODEV) 231 return NOUDEV; 232 return (x->si_udev); 233} 234 235dev_t 236udev2dev(udev_t x, int b) 237{ 238 239 if (x == NOUDEV) 240 return (NODEV); 241 switch (b) { 242 case 0: 243 return makedev(umajor(x), uminor(x)); 244 case 1: 245 return (NODEV); 246 default: 247 Debugger("udev2dev(...,X)"); 248 return NODEV; 249 } 250} 251 252int 253uminor(udev_t dev) 254{ 255 return(dev & 0xffff00ff); 256} 257 258int 259umajor(udev_t dev) 260{ 261 return((dev & 0xff00) >> 8); 262} 263 264udev_t 265makeudev(int x, int y) 266{ 267 return ((x << 8) | y); 268} 269 270dev_t 271make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...) 272{ 273 dev_t dev; 274 va_list ap; 275 int i; 276 277 KASSERT((minor & ~0xffff00ff) == 0, 278 ("Invalid minor (0x%x) in make_dev", minor)); 279 280 if (devsw->d_open == NULL) devsw->d_open = noopen; 281 if (devsw->d_close == NULL) devsw->d_close = noclose; 282 if (devsw->d_read == NULL) devsw->d_read = noread; 283 if (devsw->d_write == NULL) devsw->d_write = nowrite; 284 if (devsw->d_ioctl == NULL) devsw->d_ioctl = noioctl; 285 if (devsw->d_poll == NULL) devsw->d_poll = nopoll; 286 if (devsw->d_mmap == NULL) devsw->d_mmap = nommap; 287 if (devsw->d_strategy == NULL) devsw->d_strategy = nostrategy; 288 if (devsw->d_dump == NULL) devsw->d_dump = nodump; 289 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = nokqfilter; 290 291 if (devsw->d_maj == MAJOR_AUTO) { 292 for (i = NUMCDEVSW - 1; i > 0; i--) 293 if (reserved_majors[i] != i) 294 break; 295 KASSERT(i > 0, ("Out of major numbers (%s)", devsw->d_name)); 296 printf("Allocating major#%d to \"%s\"\n", i, devsw->d_name); 297 devsw->d_maj = i; 298 reserved_majors[i] = i; 299 } else { 300 KASSERT(devsw->d_maj >= 0 && devsw->d_maj < 256, 301 ("Invalid major (%d) in make_dev", devsw->d_maj)); 302 if (reserved_majors[devsw->d_maj] != devsw->d_maj) { 303 printf("WARNING: driver \"%s\" used %s %d\n", 304 devsw->d_name, "unreserved major device number", 305 devsw->d_maj); 306 reserved_majors[devsw->d_maj] = devsw->d_maj; 307 } 308 } 309 310 if (!ready_for_devs) { 311 printf("WARNING: Driver mistake: make_dev(%s) called before SI_SUB_DRIVERS\n", 312 fmt); 313 /* XXX panic here once drivers are cleaned up */ 314 } 315 316 dev = makedev(devsw->d_maj, minor); 317 if (dev->si_flags & SI_NAMED) { 318 printf( "WARNING: Driver mistake: repeat make_dev(\"%s\")\n", 319 dev->si_name); 320 panic("don't do that"); 321 return (dev); 322 } 323 va_start(ap, fmt); 324 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 325 if (i > (sizeof dev->__si_namebuf - 1)) { 326 printf("WARNING: Device name truncated! (%s)", 327 dev->__si_namebuf); 328 } 329 va_end(ap); 330 dev->si_devsw = devsw; 331 dev->si_uid = uid; 332 dev->si_gid = gid; 333 dev->si_mode = perms; 334 dev->si_flags |= SI_NAMED; 335 336 devfs_create(dev); 337 return (dev); 338} 339 340int 341dev_named(dev_t pdev, const char *name) 342{ 343 dev_t cdev; 344 345 if (strcmp(devtoname(pdev), name) == 0) 346 return (1); 347 LIST_FOREACH(cdev, &pdev->si_children, si_siblings) 348 if (strcmp(devtoname(cdev), name) == 0) 349 return (1); 350 return (0); 351} 352 353void 354dev_depends(dev_t pdev, dev_t cdev) 355{ 356 357 cdev->si_parent = pdev; 358 cdev->si_flags |= SI_CHILD; 359 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); 360} 361 362dev_t 363make_dev_alias(dev_t pdev, const char *fmt, ...) 364{ 365 dev_t dev; 366 va_list ap; 367 int i; 368 369 dev = allocdev(); 370 dev->si_flags |= SI_ALIAS; 371 dev->si_flags |= SI_NAMED; 372 dev_depends(pdev, dev); 373 va_start(ap, fmt); 374 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 375 if (i > (sizeof dev->__si_namebuf - 1)) { 376 printf("WARNING: Device name truncated! (%s)", 377 dev->__si_namebuf); 378 } 379 va_end(ap); 380 381 devfs_create(dev); 382 return (dev); 383} 384 385void 386revoke_and_destroy_dev(dev_t dev) 387{ 388 struct vnode *vp; 389 390 GIANT_REQUIRED; 391 392 vp = SLIST_FIRST(&dev->si_hlist); 393 if (vp != NULL) 394 VOP_REVOKE(vp, REVOKEALL); 395 destroy_dev(dev); 396} 397 398void 399destroy_dev(dev_t dev) 400{ 401 402 if (!(dev->si_flags & SI_NAMED)) { 403 printf( "WARNING: Driver mistake: destroy_dev on %d/%d\n", 404 major(dev), minor(dev)); 405 panic("don't do that"); 406 return; 407 } 408 409 devfs_destroy(dev); 410 if (dev->si_flags & SI_CHILD) { 411 LIST_REMOVE(dev, si_siblings); 412 dev->si_flags &= ~SI_CHILD; 413 } 414 while (!LIST_EMPTY(&dev->si_children)) 415 destroy_dev(LIST_FIRST(&dev->si_children)); 416 dev->si_drv1 = 0; 417 dev->si_drv2 = 0; 418 dev->si_devsw = 0; 419 bzero(&dev->__si_u, sizeof(dev->__si_u)); 420 dev->si_flags &= ~SI_NAMED; 421 dev->si_flags &= ~SI_ALIAS; 422 freedev(dev); 423} 424 425const char * 426devtoname(dev_t dev) 427{ 428 char *p; 429 int mynor; 430 431 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') { 432 p = dev->si_name; 433 if (devsw(dev)) 434 sprintf(p, "#%s/", devsw(dev)->d_name); 435 else 436 sprintf(p, "#%d/", major(dev)); 437 p += strlen(p); 438 mynor = minor(dev); 439 if (mynor < 0 || mynor > 255) 440 sprintf(p, "%#x", (u_int)mynor); 441 else 442 sprintf(p, "%d", mynor); 443 } 444 return (dev->si_name); 445} 446 447int 448dev_stdclone(char *name, char **namep, const char *stem, int *unit) 449{ 450 int u, i; 451 452 i = strlen(stem); 453 if (bcmp(stem, name, i) != 0) 454 return (0); 455 if (!isdigit(name[i])) 456 return (0); 457 u = 0; 458 if (name[i] == '0' && isdigit(name[i+1])) 459 return (0); 460 while (isdigit(name[i])) { 461 u *= 10; 462 u += name[i++] - '0'; 463 } 464 if (u > 0xffffff) 465 return (0); 466 *unit = u; 467 if (namep) 468 *namep = &name[i]; 469 if (name[i]) 470 return (2); 471 return (1); 472} 473 474/* 475 * Helper sysctl for devname(3). We're given a {u}dev_t and return 476 * the name, if any, registered by the device driver. 477 */ 478static int 479sysctl_devname(SYSCTL_HANDLER_ARGS) 480{ 481 int error; 482 udev_t ud; 483 dev_t dev; 484 485 error = SYSCTL_IN(req, &ud, sizeof (ud)); 486 if (error) 487 return (error); 488 if (ud == NOUDEV) 489 return(EINVAL); 490 dev = makedev(umajor(ud), uminor(ud)); 491 if (dev->si_name[0] == '\0') 492 error = ENOENT; 493 else 494 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1); 495 freedev(dev); 496 return (error); 497} 498 499SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 500 NULL, 0, sysctl_devname, "", "devname(3) handler"); 501 502/* 503 * Set ready_for_devs; prior to this point, device creation is not allowed. 504 */ 505static void 506dev_set_ready(void *junk) 507{ 508 ready_for_devs = 1; 509} 510 511SYSINIT(dev_ready, SI_SUB_DEVFS, SI_ORDER_FIRST, dev_set_ready, NULL);
| 110}; 111 112 113struct cdevsw * 114devsw(dev_t dev) 115{ 116 if (dev->si_devsw) 117 return (dev->si_devsw); 118 return (&dead_cdevsw); 119} 120 121/* 122 * dev_t and u_dev_t primitives 123 */ 124 125int 126major(dev_t x) 127{ 128 if (x == NODEV) 129 return NOUDEV; 130 return((x->si_udev >> 8) & 0xff); 131} 132 133int 134minor(dev_t x) 135{ 136 if (x == NODEV) 137 return NOUDEV; 138 return(x->si_udev & 0xffff00ff); 139} 140 141int 142dev2unit(dev_t x) 143{ 144 int i; 145 146 if (x == NODEV) 147 return NOUDEV; 148 i = minor(x); 149 return ((i & 0xff) | (i >> 8)); 150} 151 152int 153unit2minor(int unit) 154{ 155 156 KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit)); 157 return ((unit & 0xff) | ((unit << 8) & ~0xffff)); 158} 159 160static dev_t 161allocdev(void) 162{ 163 static int stashed; 164 struct cdev *si; 165 166 if (LIST_FIRST(&dev_free)) { 167 si = LIST_FIRST(&dev_free); 168 LIST_REMOVE(si, si_hash); 169 } else if (stashed >= DEVT_STASH) { 170 MALLOC(si, struct cdev *, sizeof(*si), M_DEVT, 171 M_USE_RESERVE | M_ZERO | M_WAITOK); 172 } else { 173 si = devt_stash + stashed++; 174 bzero(si, sizeof *si); 175 si->si_flags |= SI_STASHED; 176 } 177 si->__si_namebuf[0] = '\0'; 178 si->si_name = si->__si_namebuf; 179 LIST_INIT(&si->si_children); 180 TAILQ_INIT(&si->si_snapshots); 181 return (si); 182} 183 184dev_t 185makedev(int x, int y) 186{ 187 struct cdev *si; 188 udev_t udev; 189 int hash; 190 191 if (x == umajor(NOUDEV) && y == uminor(NOUDEV)) 192 panic("makedev of NOUDEV"); 193 udev = (x << 8) | y; 194 hash = udev % DEVT_HASH; 195 LIST_FOREACH(si, &dev_hash[hash], si_hash) { 196 if (si->si_udev == udev) 197 return (si); 198 } 199 si = allocdev(); 200 si->si_udev = udev; 201 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); 202 return (si); 203} 204 205void 206freedev(dev_t dev) 207{ 208 209 if (!free_devt) 210 return; 211 if (SLIST_FIRST(&dev->si_hlist)) 212 return; 213 if (dev->si_devsw || dev->si_drv1 || dev->si_drv2) 214 return; 215 LIST_REMOVE(dev, si_hash); 216 if (dev->si_flags & SI_STASHED) { 217 bzero(dev, sizeof(*dev)); 218 dev->si_flags |= SI_STASHED; 219 LIST_INSERT_HEAD(&dev_free, dev, si_hash); 220 } else { 221 FREE(dev, M_DEVT); 222 } 223} 224 225udev_t 226dev2udev(dev_t x) 227{ 228 if (x == NODEV) 229 return NOUDEV; 230 return (x->si_udev); 231} 232 233dev_t 234udev2dev(udev_t x, int b) 235{ 236 237 if (x == NOUDEV) 238 return (NODEV); 239 switch (b) { 240 case 0: 241 return makedev(umajor(x), uminor(x)); 242 case 1: 243 return (NODEV); 244 default: 245 Debugger("udev2dev(...,X)"); 246 return NODEV; 247 } 248} 249 250int 251uminor(udev_t dev) 252{ 253 return(dev & 0xffff00ff); 254} 255 256int 257umajor(udev_t dev) 258{ 259 return((dev & 0xff00) >> 8); 260} 261 262udev_t 263makeudev(int x, int y) 264{ 265 return ((x << 8) | y); 266} 267 268dev_t 269make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...) 270{ 271 dev_t dev; 272 va_list ap; 273 int i; 274 275 KASSERT((minor & ~0xffff00ff) == 0, 276 ("Invalid minor (0x%x) in make_dev", minor)); 277 278 if (devsw->d_open == NULL) devsw->d_open = noopen; 279 if (devsw->d_close == NULL) devsw->d_close = noclose; 280 if (devsw->d_read == NULL) devsw->d_read = noread; 281 if (devsw->d_write == NULL) devsw->d_write = nowrite; 282 if (devsw->d_ioctl == NULL) devsw->d_ioctl = noioctl; 283 if (devsw->d_poll == NULL) devsw->d_poll = nopoll; 284 if (devsw->d_mmap == NULL) devsw->d_mmap = nommap; 285 if (devsw->d_strategy == NULL) devsw->d_strategy = nostrategy; 286 if (devsw->d_dump == NULL) devsw->d_dump = nodump; 287 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = nokqfilter; 288 289 if (devsw->d_maj == MAJOR_AUTO) { 290 for (i = NUMCDEVSW - 1; i > 0; i--) 291 if (reserved_majors[i] != i) 292 break; 293 KASSERT(i > 0, ("Out of major numbers (%s)", devsw->d_name)); 294 printf("Allocating major#%d to \"%s\"\n", i, devsw->d_name); 295 devsw->d_maj = i; 296 reserved_majors[i] = i; 297 } else { 298 KASSERT(devsw->d_maj >= 0 && devsw->d_maj < 256, 299 ("Invalid major (%d) in make_dev", devsw->d_maj)); 300 if (reserved_majors[devsw->d_maj] != devsw->d_maj) { 301 printf("WARNING: driver \"%s\" used %s %d\n", 302 devsw->d_name, "unreserved major device number", 303 devsw->d_maj); 304 reserved_majors[devsw->d_maj] = devsw->d_maj; 305 } 306 } 307 308 if (!ready_for_devs) { 309 printf("WARNING: Driver mistake: make_dev(%s) called before SI_SUB_DRIVERS\n", 310 fmt); 311 /* XXX panic here once drivers are cleaned up */ 312 } 313 314 dev = makedev(devsw->d_maj, minor); 315 if (dev->si_flags & SI_NAMED) { 316 printf( "WARNING: Driver mistake: repeat make_dev(\"%s\")\n", 317 dev->si_name); 318 panic("don't do that"); 319 return (dev); 320 } 321 va_start(ap, fmt); 322 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 323 if (i > (sizeof dev->__si_namebuf - 1)) { 324 printf("WARNING: Device name truncated! (%s)", 325 dev->__si_namebuf); 326 } 327 va_end(ap); 328 dev->si_devsw = devsw; 329 dev->si_uid = uid; 330 dev->si_gid = gid; 331 dev->si_mode = perms; 332 dev->si_flags |= SI_NAMED; 333 334 devfs_create(dev); 335 return (dev); 336} 337 338int 339dev_named(dev_t pdev, const char *name) 340{ 341 dev_t cdev; 342 343 if (strcmp(devtoname(pdev), name) == 0) 344 return (1); 345 LIST_FOREACH(cdev, &pdev->si_children, si_siblings) 346 if (strcmp(devtoname(cdev), name) == 0) 347 return (1); 348 return (0); 349} 350 351void 352dev_depends(dev_t pdev, dev_t cdev) 353{ 354 355 cdev->si_parent = pdev; 356 cdev->si_flags |= SI_CHILD; 357 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); 358} 359 360dev_t 361make_dev_alias(dev_t pdev, const char *fmt, ...) 362{ 363 dev_t dev; 364 va_list ap; 365 int i; 366 367 dev = allocdev(); 368 dev->si_flags |= SI_ALIAS; 369 dev->si_flags |= SI_NAMED; 370 dev_depends(pdev, dev); 371 va_start(ap, fmt); 372 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 373 if (i > (sizeof dev->__si_namebuf - 1)) { 374 printf("WARNING: Device name truncated! (%s)", 375 dev->__si_namebuf); 376 } 377 va_end(ap); 378 379 devfs_create(dev); 380 return (dev); 381} 382 383void 384revoke_and_destroy_dev(dev_t dev) 385{ 386 struct vnode *vp; 387 388 GIANT_REQUIRED; 389 390 vp = SLIST_FIRST(&dev->si_hlist); 391 if (vp != NULL) 392 VOP_REVOKE(vp, REVOKEALL); 393 destroy_dev(dev); 394} 395 396void 397destroy_dev(dev_t dev) 398{ 399 400 if (!(dev->si_flags & SI_NAMED)) { 401 printf( "WARNING: Driver mistake: destroy_dev on %d/%d\n", 402 major(dev), minor(dev)); 403 panic("don't do that"); 404 return; 405 } 406 407 devfs_destroy(dev); 408 if (dev->si_flags & SI_CHILD) { 409 LIST_REMOVE(dev, si_siblings); 410 dev->si_flags &= ~SI_CHILD; 411 } 412 while (!LIST_EMPTY(&dev->si_children)) 413 destroy_dev(LIST_FIRST(&dev->si_children)); 414 dev->si_drv1 = 0; 415 dev->si_drv2 = 0; 416 dev->si_devsw = 0; 417 bzero(&dev->__si_u, sizeof(dev->__si_u)); 418 dev->si_flags &= ~SI_NAMED; 419 dev->si_flags &= ~SI_ALIAS; 420 freedev(dev); 421} 422 423const char * 424devtoname(dev_t dev) 425{ 426 char *p; 427 int mynor; 428 429 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') { 430 p = dev->si_name; 431 if (devsw(dev)) 432 sprintf(p, "#%s/", devsw(dev)->d_name); 433 else 434 sprintf(p, "#%d/", major(dev)); 435 p += strlen(p); 436 mynor = minor(dev); 437 if (mynor < 0 || mynor > 255) 438 sprintf(p, "%#x", (u_int)mynor); 439 else 440 sprintf(p, "%d", mynor); 441 } 442 return (dev->si_name); 443} 444 445int 446dev_stdclone(char *name, char **namep, const char *stem, int *unit) 447{ 448 int u, i; 449 450 i = strlen(stem); 451 if (bcmp(stem, name, i) != 0) 452 return (0); 453 if (!isdigit(name[i])) 454 return (0); 455 u = 0; 456 if (name[i] == '0' && isdigit(name[i+1])) 457 return (0); 458 while (isdigit(name[i])) { 459 u *= 10; 460 u += name[i++] - '0'; 461 } 462 if (u > 0xffffff) 463 return (0); 464 *unit = u; 465 if (namep) 466 *namep = &name[i]; 467 if (name[i]) 468 return (2); 469 return (1); 470} 471 472/* 473 * Helper sysctl for devname(3). We're given a {u}dev_t and return 474 * the name, if any, registered by the device driver. 475 */ 476static int 477sysctl_devname(SYSCTL_HANDLER_ARGS) 478{ 479 int error; 480 udev_t ud; 481 dev_t dev; 482 483 error = SYSCTL_IN(req, &ud, sizeof (ud)); 484 if (error) 485 return (error); 486 if (ud == NOUDEV) 487 return(EINVAL); 488 dev = makedev(umajor(ud), uminor(ud)); 489 if (dev->si_name[0] == '\0') 490 error = ENOENT; 491 else 492 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1); 493 freedev(dev); 494 return (error); 495} 496 497SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 498 NULL, 0, sysctl_devname, "", "devname(3) handler"); 499 500/* 501 * Set ready_for_devs; prior to this point, device creation is not allowed. 502 */ 503static void 504dev_set_ready(void *junk) 505{ 506 ready_for_devs = 1; 507} 508 509SYSINIT(dev_ready, SI_SUB_DEVFS, SI_ORDER_FIRST, dev_set_ready, NULL);
|