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 devvn_refthread(struct vnode *vp, struct cdev **devp)
133 mtx_assert(&devmtx, MA_NOTOWNED);
138 csw = (*devp)->si_devsw;
140 (*devp)->si_threadcount++;
147 dev_relthread(struct cdev *dev)
150 mtx_assert(&devmtx, MA_NOTOWNED);
152 dev->si_threadcount--;
182 /* Define a dead_cdevsw for use when devices leave unexpectedly. */
184 #define dead_open (d_open_t *)enxio
185 #define dead_close (d_close_t *)enxio
186 #define dead_read (d_read_t *)enxio
187 #define dead_write (d_write_t *)enxio
188 #define dead_ioctl (d_ioctl_t *)enxio
189 #define dead_poll (d_poll_t *)enodev
190 #define dead_mmap (d_mmap_t *)enodev
193 dead_strategy(struct bio *bp)
196 biofinish(bp, NULL, ENXIO);
199 #define dead_dump (dumper_t *)enxio
200 #define dead_kqfilter (d_kqfilter_t *)enxio
202 static struct cdevsw dead_cdevsw = {
203 .d_version = D_VERSION,
204 .d_flags = D_NEEDGIANT, /* XXX: does dead_strategy need this ? */
206 .d_close = dead_close,
208 .d_write = dead_write,
209 .d_ioctl = dead_ioctl,
212 .d_strategy = dead_strategy,
215 .d_kqfilter = dead_kqfilter
218 /* Default methods if driver does not specify method */
220 #define null_open (d_open_t *)nullop
221 #define null_close (d_close_t *)nullop
222 #define no_read (d_read_t *)enodev
223 #define no_write (d_write_t *)enodev
224 #define no_ioctl (d_ioctl_t *)enodev
225 #define no_mmap (d_mmap_t *)enodev
226 #define no_kqfilter (d_kqfilter_t *)enodev
229 no_strategy(struct bio *bp)
232 biofinish(bp, NULL, ENODEV);
236 no_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
239 * Return true for read/write. If the user asked for something
240 * special, return POLLNVAL, so that clients have a way of
241 * determining reliably whether or not the extended
242 * functionality is present without hard-coding knowledge
243 * of specific filesystem implementations.
244 * Stay in sync with vop_nopoll().
246 if (events & ~POLLSTANDARD)
249 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
252 #define no_dump (dumper_t *)enodev
255 giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
260 dsw = dev_refthread(dev);
264 retval = dsw->d_gianttrick->d_open(dev, oflags, devtype, td);
271 giant_fdopen(struct cdev *dev, int oflags, struct thread *td, int fdidx)
276 dsw = dev_refthread(dev);
280 retval = dsw->d_gianttrick->d_fdopen(dev, oflags, td, fdidx);
287 giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
292 dsw = dev_refthread(dev);
296 retval = dsw->d_gianttrick->d_close(dev, fflag, devtype, td);
303 giant_strategy(struct bio *bp)
309 dsw = dev_refthread(dev);
311 biofinish(bp, NULL, ENXIO);
315 dsw->d_gianttrick->d_strategy(bp);
321 giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
326 dsw = dev_refthread(dev);
330 retval = dsw->d_gianttrick->d_ioctl(dev, cmd, data, fflag, td);
337 giant_read(struct cdev *dev, struct uio *uio, int ioflag)
342 dsw = dev_refthread(dev);
346 retval = dsw->d_gianttrick->d_read(dev, uio, ioflag);
353 giant_write(struct cdev *dev, struct uio *uio, int ioflag)
358 dsw = dev_refthread(dev);
362 retval = dsw->d_gianttrick->d_write(dev, uio, ioflag);
369 giant_poll(struct cdev *dev, int events, struct thread *td)
374 dsw = dev_refthread(dev);
378 retval = dsw->d_gianttrick->d_poll(dev, events, td);
385 giant_kqfilter(struct cdev *dev, struct knote *kn)
390 dsw = dev_refthread(dev);
394 retval = dsw->d_gianttrick->d_kqfilter(dev, kn);
401 giant_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
406 dsw = dev_refthread(dev);
410 retval = dsw->d_gianttrick->d_mmap(dev, offset, paddr, nprot);
418 * struct cdev * and u_dev_t primitives
422 minor(struct cdev *x)
426 return(x->si_drv0 & MAXMINOR);
430 dev2unit(struct cdev *x)
435 return (minor2unit(minor(x)));
439 minor2unit(u_int _minor)
442 KASSERT((_minor & ~MAXMINOR) == 0, ("Illegal minor %x", _minor));
443 return ((_minor & 0xff) | ((_minor >> 8) & 0xffff00));
450 KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit));
451 return ((unit & 0xff) | ((unit << 8) & ~0xffff));
455 newdev(struct cdevsw *csw, int y, struct cdev *si)
460 mtx_assert(&devmtx, MA_OWNED);
462 LIST_FOREACH(si2, &csw->d_devs, si_list) {
463 if (si2->si_drv0 == udev) {
470 LIST_INSERT_HEAD(&csw->d_devs, si, si_list);
477 return (dev & MAXMINOR);
483 return ((dev & ~MAXMINOR) >> 8);
487 fini_cdevsw(struct cdevsw *devsw)
491 if (devsw->d_gianttrick != NULL) {
492 gt = devsw->d_gianttrick;
493 memcpy(devsw, gt, sizeof *devsw);
495 devsw->d_gianttrick = NULL;
497 devsw->d_flags &= ~D_INIT;
501 prep_cdevsw(struct cdevsw *devsw)
505 if (devsw->d_flags & D_NEEDGIANT)
506 dsw2 = malloc(sizeof *dsw2, M_DEVT, M_WAITOK);
511 if (devsw->d_version != D_VERSION_01) {
513 "WARNING: Device driver \"%s\" has wrong version %s\n",
514 devsw->d_name == NULL ? "???" : devsw->d_name,
515 "and is disabled. Recompile KLD module.");
516 devsw->d_open = dead_open;
517 devsw->d_close = dead_close;
518 devsw->d_read = dead_read;
519 devsw->d_write = dead_write;
520 devsw->d_ioctl = dead_ioctl;
521 devsw->d_poll = dead_poll;
522 devsw->d_mmap = dead_mmap;
523 devsw->d_strategy = dead_strategy;
524 devsw->d_dump = dead_dump;
525 devsw->d_kqfilter = dead_kqfilter;
528 if (devsw->d_flags & D_TTY) {
529 if (devsw->d_ioctl == NULL) devsw->d_ioctl = ttyioctl;
530 if (devsw->d_read == NULL) devsw->d_read = ttyread;
531 if (devsw->d_write == NULL) devsw->d_write = ttywrite;
532 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = ttykqfilter;
533 if (devsw->d_poll == NULL) devsw->d_poll = ttypoll;
536 if (devsw->d_flags & D_NEEDGIANT) {
537 if (devsw->d_gianttrick == NULL) {
538 memcpy(dsw2, devsw, sizeof *dsw2);
539 devsw->d_gianttrick = dsw2;
544 #define FIXUP(member, noop, giant) \
546 if (devsw->member == NULL) { \
547 devsw->member = noop; \
548 } else if (devsw->d_flags & D_NEEDGIANT) \
549 devsw->member = giant; \
553 FIXUP(d_open, null_open, giant_open);
554 FIXUP(d_fdopen, NULL, giant_fdopen);
555 FIXUP(d_close, null_close, giant_close);
556 FIXUP(d_read, no_read, giant_read);
557 FIXUP(d_write, no_write, giant_write);
558 FIXUP(d_ioctl, no_ioctl, giant_ioctl);
559 FIXUP(d_poll, no_poll, giant_poll);
560 FIXUP(d_mmap, no_mmap, giant_mmap);
561 FIXUP(d_strategy, no_strategy, giant_strategy);
562 FIXUP(d_kqfilter, no_kqfilter, giant_kqfilter);
564 if (devsw->d_dump == NULL) devsw->d_dump = no_dump;
566 LIST_INIT(&devsw->d_devs);
568 devsw->d_flags |= D_INIT;
574 make_dev_credv(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid,
575 gid_t gid, int mode, const char *fmt, va_list ap)
580 KASSERT((minornr & ~MAXMINOR) == 0,
581 ("Invalid minor (0x%x) in make_dev", minornr));
583 if (!(devsw->d_flags & D_INIT))
587 dev = newdev(devsw, minornr, dev);
588 if (dev->si_flags & SI_CHEAPCLONE &&
589 dev->si_flags & SI_NAMED) {
591 * This is allowed as it removes races and generally
592 * simplifies cloning devices.
598 KASSERT(!(dev->si_flags & SI_NAMED),
599 ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)",
600 devsw->d_name, minor(dev), devtoname(dev)));
602 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
603 if (i > (sizeof dev->__si_namebuf - 1)) {
604 printf("WARNING: Device name truncated! (%s)\n",
608 dev->si_flags |= SI_NAMED;
611 dev->si_cred = crhold(cr);
625 make_dev(struct cdevsw *devsw, int minornr, uid_t uid, gid_t gid, int mode,
626 const char *fmt, ...)
632 dev = make_dev_credv(devsw, minornr, NULL, uid, gid, mode, fmt, ap);
638 make_dev_cred(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid,
639 gid_t gid, int mode, const char *fmt, ...)
645 dev = make_dev_credv(devsw, minornr, cr, uid, gid, mode, fmt, ap);
652 dev_dependsl(struct cdev *pdev, struct cdev *cdev)
655 cdev->si_parent = pdev;
656 cdev->si_flags |= SI_CHILD;
657 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
662 dev_depends(struct cdev *pdev, struct cdev *cdev)
666 dev_dependsl(pdev, cdev);
671 make_dev_alias(struct cdev *pdev, const char *fmt, ...)
679 dev->si_flags |= SI_ALIAS;
680 dev->si_flags |= SI_NAMED;
682 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
683 if (i > (sizeof dev->__si_namebuf - 1)) {
684 printf("WARNING: Device name truncated! (%s)\n",
691 dev_depends(pdev, dev);
696 destroy_devl(struct cdev *dev)
700 mtx_assert(&devmtx, MA_OWNED);
701 KASSERT(dev->si_flags & SI_NAMED,
702 ("WARNING: Driver mistake: destroy_dev on %d\n", minor(dev)));
706 /* Remove name marking */
707 dev->si_flags &= ~SI_NAMED;
709 /* If we are a child, remove us from the parents list */
710 if (dev->si_flags & SI_CHILD) {
711 LIST_REMOVE(dev, si_siblings);
712 dev->si_flags &= ~SI_CHILD;
715 /* Kill our children */
716 while (!LIST_EMPTY(&dev->si_children))
717 destroy_devl(LIST_FIRST(&dev->si_children));
719 /* Remove from clone list */
720 if (dev->si_flags & SI_CLONELIST) {
721 LIST_REMOVE(dev, si_clone);
722 dev->si_flags &= ~SI_CLONELIST;
726 dev->si_devsw = NULL; /* already NULL for SI_ALIAS */
727 while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) {
728 printf("Purging %lu threads from %s\n",
729 dev->si_threadcount, devtoname(dev));
731 msleep(csw, &devmtx, PRIBIO, "devprg", hz/10);
733 if (csw != NULL && csw->d_purge != NULL)
734 printf("All threads purged from %s\n", devtoname(dev));
738 bzero(&dev->__si_u, sizeof(dev->__si_u));
740 if (!(dev->si_flags & SI_ALIAS)) {
741 /* Remove from cdevsw list */
742 LIST_REMOVE(dev, si_list);
744 /* If cdevsw has no more struct cdev *'s, clean it */
745 if (LIST_EMPTY(&csw->d_devs))
748 dev->si_flags &= ~SI_ALIAS;
750 if (dev->si_refcount > 0) {
751 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list);
758 destroy_dev(struct cdev *dev)
767 devtoname(struct cdev *dev)
773 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
775 csw = dev_refthread(dev);
777 sprintf(p, "(%s)", csw->d_name);
782 if (mynor < 0 || mynor > 255)
783 sprintf(p, "/%#x", (u_int)mynor);
785 sprintf(p, "/%d", mynor);
787 return (dev->si_name);
791 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
796 if (bcmp(stem, name, i) != 0)
798 if (!isdigit(name[i]))
801 if (name[i] == '0' && isdigit(name[i+1]))
803 while (isdigit(name[i])) {
805 u += name[i++] - '0';
818 * Helper functions for cloning device drivers.
820 * The objective here is to make it unnecessary for the device drivers to
821 * use rman or similar to manage their unit number space. Due to the way
822 * we do "on-demand" devices, using rman or other "private" methods
823 * will be very tricky to lock down properly once we lock down this file.
825 * Instead we give the drivers these routines which puts the struct cdev *'s
826 * that are to be managed on their own list, and gives the driver the ability
827 * to ask for the first free unit number or a given specified unit number.
829 * In addition these routines support paired devices (pty, nmdm and similar)
830 * by respecting a number of "flag" bits in the minor number.
835 LIST_HEAD(,cdev) head;
839 clone_setup(struct clonedevs **cdp)
842 *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO);
843 LIST_INIT(&(*cdp)->head);
847 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, struct cdev **dp, int extra)
849 struct clonedevs *cd;
850 struct cdev *dev, *ndev, *dl, *de;
853 KASSERT(*cdp != NULL,
854 ("clone_setup() not called in driver \"%s\"", csw->d_name));
855 KASSERT(!(extra & CLONE_UNITMASK),
856 ("Illegal extra bits (0x%x) in clone_create", extra));
857 KASSERT(*up <= CLONE_UNITMASK,
858 ("Too high unit (0x%x) in clone_create", *up));
860 if (!(csw->d_flags & D_INIT))
864 * Search the list for a lot of things in one go:
865 * A preexisting match is returned immediately.
866 * The lowest free unit number if we are passed -1, and the place
867 * in the list where we should insert that new element.
868 * The place to insert a specified unit number, if applicable
869 * the end of the list.
872 ndev = devfs_alloc();
877 LIST_FOREACH(dev, &cd->head, si_clone) {
878 KASSERT(dev->si_flags & SI_CLONELIST,
879 ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
881 if (u == (unit | extra)) {
887 if (unit == -1 && u == low) {
891 } else if (u < (unit | extra)) {
894 } else if (u > (unit | extra)) {
900 unit = low & CLONE_UNITMASK;
901 dev = newdev(csw, unit2minor(unit | extra), ndev);
902 if (dev->si_flags & SI_CLONELIST) {
903 printf("dev %p (%s) is on clonelist\n", dev, dev->si_name);
904 printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra);
905 LIST_FOREACH(dev, &cd->head, si_clone) {
906 printf("\t%p %s\n", dev, dev->si_name);
910 KASSERT(!(dev->si_flags & SI_CLONELIST),
911 ("Dev %p(%s) should not be on clonelist", dev, dev->si_name));
913 LIST_INSERT_BEFORE(dl, dev, si_clone);
915 LIST_INSERT_AFTER(de, dev, si_clone);
917 LIST_INSERT_HEAD(&cd->head, dev, si_clone);
918 dev->si_flags |= SI_CLONELIST;
925 * Kill everything still on the list. The driver should already have
926 * disposed of any softc hung of the struct cdev *'s at this time.
929 clone_cleanup(struct clonedevs **cdp)
931 struct cdev *dev, *tdev;
932 struct clonedevs *cd;
938 LIST_FOREACH_SAFE(dev, &cd->head, si_clone, tdev) {
939 KASSERT(dev->si_flags & SI_CLONELIST,
940 ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
941 KASSERT(dev->si_flags & SI_NAMED,
942 ("Driver has goofed in cloning underways udev %x", dev->si_drv0));