2 * Copyright (c) 1999-2002 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
35 #include <sys/mutex.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
39 #include <sys/vnode.h>
40 #include <sys/queue.h>
42 #include <sys/ctype.h>
44 #include <sys/ucred.h>
45 #include <machine/stdarg.h>
47 #include <fs/devfs/devfs_int.h>
49 static MALLOC_DEFINE(M_DEVT, "cdev", "cdev storage");
52 static void destroy_devl(struct cdev *dev);
53 static struct cdev *make_dev_credv(struct cdevsw *devsw, int minornr,
54 struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,
72 dev_ref(struct cdev *dev)
75 mtx_assert(&devmtx, MA_NOTOWNED);
82 dev_refl(struct cdev *dev)
85 mtx_assert(&devmtx, MA_OWNED);
90 dev_rel(struct cdev *dev)
94 mtx_assert(&devmtx, MA_NOTOWNED);
97 KASSERT(dev->si_refcount >= 0,
98 ("dev_rel(%s) gave negative count", devtoname(dev)));
100 if (dev->si_usecount == 0 &&
101 (dev->si_flags & SI_CHEAPCLONE) && (dev->si_flags & SI_NAMED))
105 if (dev->si_devsw == NULL && dev->si_refcount == 0) {
106 LIST_REMOVE(dev, si_list);
115 dev_refthread(struct cdev *dev)
119 mtx_assert(&devmtx, MA_NOTOWNED);
123 dev->si_threadcount++;
129 dev_relthread(struct cdev *dev)
132 mtx_assert(&devmtx, MA_NOTOWNED);
134 dev->si_threadcount--;
164 /* Define a dead_cdevsw for use when devices leave unexpectedly. */
166 #define dead_open (d_open_t *)enxio
167 #define dead_close (d_close_t *)enxio
168 #define dead_read (d_read_t *)enxio
169 #define dead_write (d_write_t *)enxio
170 #define dead_ioctl (d_ioctl_t *)enxio
171 #define dead_poll (d_poll_t *)enodev
172 #define dead_mmap (d_mmap_t *)enodev
175 dead_strategy(struct bio *bp)
178 biofinish(bp, NULL, ENXIO);
181 #define dead_dump (dumper_t *)enxio
182 #define dead_kqfilter (d_kqfilter_t *)enxio
184 static struct cdevsw dead_cdevsw = {
185 .d_version = D_VERSION,
186 .d_flags = D_NEEDGIANT, /* XXX: does dead_strategy need this ? */
188 .d_close = dead_close,
190 .d_write = dead_write,
191 .d_ioctl = dead_ioctl,
194 .d_strategy = dead_strategy,
197 .d_kqfilter = dead_kqfilter
200 /* Default methods if driver does not specify method */
202 #define null_open (d_open_t *)nullop
203 #define null_close (d_close_t *)nullop
204 #define no_read (d_read_t *)enodev
205 #define no_write (d_write_t *)enodev
206 #define no_ioctl (d_ioctl_t *)enodev
207 #define no_mmap (d_mmap_t *)enodev
208 #define no_kqfilter (d_kqfilter_t *)enodev
211 no_strategy(struct bio *bp)
214 biofinish(bp, NULL, ENODEV);
218 no_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
221 * Return true for read/write. If the user asked for something
222 * special, return POLLNVAL, so that clients have a way of
223 * determining reliably whether or not the extended
224 * functionality is present without hard-coding knowledge
225 * of specific filesystem implementations.
226 * Stay in sync with vop_nopoll().
228 if (events & ~POLLSTANDARD)
231 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
234 #define no_dump (dumper_t *)enodev
237 giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
242 retval = dev->si_devsw->d_gianttrick->
243 d_open(dev, oflags, devtype, td);
249 giant_fdopen(struct cdev *dev, int oflags, struct thread *td, int fdidx)
254 retval = dev->si_devsw->d_gianttrick->
255 d_fdopen(dev, oflags, td, fdidx);
261 giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
266 retval = dev->si_devsw->d_gianttrick->
267 d_close(dev, fflag, devtype, td);
273 giant_strategy(struct bio *bp)
277 bp->bio_dev->si_devsw->d_gianttrick->
283 giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
288 retval = dev->si_devsw->d_gianttrick->
289 d_ioctl(dev, cmd, data, fflag, td);
295 giant_read(struct cdev *dev, struct uio *uio, int ioflag)
300 retval = dev->si_devsw->d_gianttrick->
301 d_read(dev, uio, ioflag);
307 giant_write(struct cdev *dev, struct uio *uio, int ioflag)
312 retval = dev->si_devsw->d_gianttrick->
313 d_write(dev, uio, ioflag);
319 giant_poll(struct cdev *dev, int events, struct thread *td)
324 retval = dev->si_devsw->d_gianttrick->
325 d_poll(dev, events, td);
331 giant_kqfilter(struct cdev *dev, struct knote *kn)
336 retval = dev->si_devsw->d_gianttrick->
343 giant_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
348 retval = dev->si_devsw->d_gianttrick->
349 d_mmap(dev, offset, paddr, nprot);
356 * struct cdev * and u_dev_t primitives
360 minor(struct cdev *x)
364 return(x->si_drv0 & MAXMINOR);
368 dev2unit(struct cdev *x)
373 return (minor2unit(minor(x)));
377 minor2unit(u_int _minor)
380 KASSERT((_minor & ~MAXMINOR) == 0, ("Illegal minor %x", _minor));
381 return ((_minor & 0xff) | ((_minor >> 8) & 0xffff00));
388 KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit));
389 return ((unit & 0xff) | ((unit << 8) & ~0xffff));
393 newdev(struct cdevsw *csw, int y, struct cdev *si)
398 mtx_assert(&devmtx, MA_OWNED);
400 LIST_FOREACH(si2, &csw->d_devs, si_list) {
401 if (si2->si_drv0 == udev) {
408 LIST_INSERT_HEAD(&csw->d_devs, si, si_list);
415 return (dev & MAXMINOR);
421 return ((dev & ~MAXMINOR) >> 8);
425 fini_cdevsw(struct cdevsw *devsw)
429 if (devsw->d_gianttrick != NULL) {
430 gt = devsw->d_gianttrick;
431 memcpy(devsw, gt, sizeof *devsw);
433 devsw->d_gianttrick = NULL;
435 devsw->d_flags &= ~D_INIT;
439 prep_cdevsw(struct cdevsw *devsw)
443 if (devsw->d_flags & D_NEEDGIANT)
444 dsw2 = malloc(sizeof *dsw2, M_DEVT, M_WAITOK);
449 if (devsw->d_version != D_VERSION_01) {
451 "WARNING: Device driver \"%s\" has wrong version %s\n",
452 devsw->d_name == NULL ? "???" : devsw->d_name,
453 "and is disabled. Recompile KLD module.");
454 devsw->d_open = dead_open;
455 devsw->d_close = dead_close;
456 devsw->d_read = dead_read;
457 devsw->d_write = dead_write;
458 devsw->d_ioctl = dead_ioctl;
459 devsw->d_poll = dead_poll;
460 devsw->d_mmap = dead_mmap;
461 devsw->d_strategy = dead_strategy;
462 devsw->d_dump = dead_dump;
463 devsw->d_kqfilter = dead_kqfilter;
466 if (devsw->d_flags & D_TTY) {
467 if (devsw->d_ioctl == NULL) devsw->d_ioctl = ttyioctl;
468 if (devsw->d_read == NULL) devsw->d_read = ttyread;
469 if (devsw->d_write == NULL) devsw->d_write = ttywrite;
470 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = ttykqfilter;
471 if (devsw->d_poll == NULL) devsw->d_poll = ttypoll;
474 if (devsw->d_flags & D_NEEDGIANT) {
475 if (devsw->d_gianttrick == NULL) {
476 memcpy(dsw2, devsw, sizeof *dsw2);
477 devsw->d_gianttrick = dsw2;
482 #define FIXUP(member, noop, giant) \
484 if (devsw->member == NULL) { \
485 devsw->member = noop; \
486 } else if (devsw->d_flags & D_NEEDGIANT) \
487 devsw->member = giant; \
491 FIXUP(d_open, null_open, giant_open);
492 FIXUP(d_fdopen, NULL, giant_fdopen);
493 FIXUP(d_close, null_close, giant_close);
494 FIXUP(d_read, no_read, giant_read);
495 FIXUP(d_write, no_write, giant_write);
496 FIXUP(d_ioctl, no_ioctl, giant_ioctl);
497 FIXUP(d_poll, no_poll, giant_poll);
498 FIXUP(d_mmap, no_mmap, giant_mmap);
499 FIXUP(d_strategy, no_strategy, giant_strategy);
500 FIXUP(d_kqfilter, no_kqfilter, giant_kqfilter);
502 if (devsw->d_dump == NULL) devsw->d_dump = no_dump;
504 LIST_INIT(&devsw->d_devs);
506 devsw->d_flags |= D_INIT;
512 make_dev_credv(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid,
513 gid_t gid, int mode, const char *fmt, va_list ap)
518 KASSERT((minornr & ~MAXMINOR) == 0,
519 ("Invalid minor (0x%x) in make_dev", minornr));
521 if (!(devsw->d_flags & D_INIT))
525 dev = newdev(devsw, minornr, dev);
526 if (dev->si_flags & SI_CHEAPCLONE &&
527 dev->si_flags & SI_NAMED) {
529 * This is allowed as it removes races and generally
530 * simplifies cloning devices.
536 KASSERT(!(dev->si_flags & SI_NAMED),
537 ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)",
538 devsw->d_name, minor(dev), devtoname(dev)));
540 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
541 if (i > (sizeof dev->__si_namebuf - 1)) {
542 printf("WARNING: Device name truncated! (%s)\n",
546 dev->si_flags |= SI_NAMED;
548 dev->si_cred = crhold(cr);
561 make_dev(struct cdevsw *devsw, int minornr, uid_t uid, gid_t gid, int mode,
562 const char *fmt, ...)
568 dev = make_dev_credv(devsw, minornr, NULL, uid, gid, mode, fmt, ap);
574 make_dev_cred(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid,
575 gid_t gid, int mode, const char *fmt, ...)
581 dev = make_dev_credv(devsw, minornr, cr, uid, gid, mode, fmt, ap);
588 dev_dependsl(struct cdev *pdev, struct cdev *cdev)
591 cdev->si_parent = pdev;
592 cdev->si_flags |= SI_CHILD;
593 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
598 dev_depends(struct cdev *pdev, struct cdev *cdev)
602 dev_dependsl(pdev, cdev);
607 make_dev_alias(struct cdev *pdev, const char *fmt, ...)
615 dev->si_flags |= SI_ALIAS;
616 dev->si_flags |= SI_NAMED;
618 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
619 if (i > (sizeof dev->__si_namebuf - 1)) {
620 printf("WARNING: Device name truncated! (%s)\n",
627 dev_depends(pdev, dev);
632 destroy_devl(struct cdev *dev)
636 mtx_assert(&devmtx, MA_OWNED);
637 KASSERT(dev->si_flags & SI_NAMED,
638 ("WARNING: Driver mistake: destroy_dev on %d\n", minor(dev)));
642 /* Remove name marking */
643 dev->si_flags &= ~SI_NAMED;
645 /* If we are a child, remove us from the parents list */
646 if (dev->si_flags & SI_CHILD) {
647 LIST_REMOVE(dev, si_siblings);
648 dev->si_flags &= ~SI_CHILD;
651 /* Kill our children */
652 while (!LIST_EMPTY(&dev->si_children))
653 destroy_devl(LIST_FIRST(&dev->si_children));
655 /* Remove from clone list */
656 if (dev->si_flags & SI_CLONELIST) {
657 LIST_REMOVE(dev, si_clone);
658 dev->si_flags &= ~SI_CLONELIST;
662 dev->si_devsw = NULL; /* already NULL for SI_ALIAS */
663 while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) {
664 printf("Purging %lu threads from %s\n",
665 dev->si_threadcount, devtoname(dev));
667 msleep(csw, &devmtx, PRIBIO, "devprg", hz/10);
669 if (csw != NULL && csw->d_purge != NULL)
670 printf("All threads purged from %s\n", devtoname(dev));
674 bzero(&dev->__si_u, sizeof(dev->__si_u));
676 if (!(dev->si_flags & SI_ALIAS)) {
677 /* Remove from cdevsw list */
678 LIST_REMOVE(dev, si_list);
680 /* If cdevsw has no more struct cdev *'s, clean it */
681 if (LIST_EMPTY(&csw->d_devs))
684 dev->si_flags &= ~SI_ALIAS;
686 if (dev->si_refcount > 0) {
687 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list);
694 destroy_dev(struct cdev *dev)
703 devtoname(struct cdev *dev)
709 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
711 csw = dev_refthread(dev);
713 sprintf(p, "(%s)", csw->d_name);
718 if (mynor < 0 || mynor > 255)
719 sprintf(p, "/%#x", (u_int)mynor);
721 sprintf(p, "/%d", mynor);
723 return (dev->si_name);
727 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
732 if (bcmp(stem, name, i) != 0)
734 if (!isdigit(name[i]))
737 if (name[i] == '0' && isdigit(name[i+1]))
739 while (isdigit(name[i])) {
741 u += name[i++] - '0';
754 * Helper functions for cloning device drivers.
756 * The objective here is to make it unnecessary for the device drivers to
757 * use rman or similar to manage their unit number space. Due to the way
758 * we do "on-demand" devices, using rman or other "private" methods
759 * will be very tricky to lock down properly once we lock down this file.
761 * Instead we give the drivers these routines which puts the struct cdev *'s
762 * that are to be managed on their own list, and gives the driver the ability
763 * to ask for the first free unit number or a given specified unit number.
765 * In addition these routines support paired devices (pty, nmdm and similar)
766 * by respecting a number of "flag" bits in the minor number.
771 LIST_HEAD(,cdev) head;
775 clone_setup(struct clonedevs **cdp)
778 *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO);
779 LIST_INIT(&(*cdp)->head);
783 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, struct cdev **dp, u_int extra)
785 struct clonedevs *cd;
786 struct cdev *dev, *ndev, *dl, *de;
789 KASSERT(*cdp != NULL,
790 ("clone_setup() not called in driver \"%s\"", csw->d_name));
791 KASSERT(!(extra & CLONE_UNITMASK),
792 ("Illegal extra bits (0x%x) in clone_create", extra));
793 KASSERT(*up <= CLONE_UNITMASK,
794 ("Too high unit (0x%x) in clone_create", *up));
796 if (!(csw->d_flags & D_INIT))
800 * Search the list for a lot of things in one go:
801 * A preexisting match is returned immediately.
802 * The lowest free unit number if we are passed -1, and the place
803 * in the list where we should insert that new element.
804 * The place to insert a specified unit number, if applicable
805 * the end of the list.
808 ndev = devfs_alloc();
813 LIST_FOREACH(dev, &cd->head, si_clone) {
814 KASSERT(dev->si_flags & SI_CLONELIST,
815 ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
817 if (u == (unit | extra)) {
823 if (unit == -1 && u == low) {
827 } else if (u < (unit | extra)) {
830 } else if (u > (unit | extra)) {
836 unit = low & CLONE_UNITMASK;
837 dev = newdev(csw, unit2minor(unit | extra), ndev);
838 if (dev->si_flags & SI_CLONELIST) {
839 printf("dev %p (%s) is on clonelist\n", dev, dev->si_name);
840 printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra);
841 LIST_FOREACH(dev, &cd->head, si_clone) {
842 printf("\t%p %s\n", dev, dev->si_name);
846 KASSERT(!(dev->si_flags & SI_CLONELIST),
847 ("Dev %p(%s) should not be on clonelist", dev, dev->si_name));
849 LIST_INSERT_BEFORE(dl, dev, si_clone);
851 LIST_INSERT_AFTER(de, dev, si_clone);
853 LIST_INSERT_HEAD(&cd->head, dev, si_clone);
854 dev->si_flags |= SI_CLONELIST;
861 * Kill everything still on the list. The driver should already have
862 * disposed of any softc hung of the struct cdev *'s at this time.
865 clone_cleanup(struct clonedevs **cdp)
867 struct cdev *dev, *tdev;
868 struct clonedevs *cd;
874 LIST_FOREACH_SAFE(dev, &cd->head, si_clone, tdev) {
875 KASSERT(dev->si_flags & SI_CLONELIST,
876 ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
877 KASSERT(dev->si_flags & SI_NAMED,
878 ("Driver has goofed in cloning underways udev %x", dev->si_drv0));