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, "and is disabled. Recompile KLD module.");
453 devsw->d_open = dead_open;
454 devsw->d_close = dead_close;
455 devsw->d_read = dead_read;
456 devsw->d_write = dead_write;
457 devsw->d_ioctl = dead_ioctl;
458 devsw->d_poll = dead_poll;
459 devsw->d_mmap = dead_mmap;
460 devsw->d_strategy = dead_strategy;
461 devsw->d_dump = dead_dump;
462 devsw->d_kqfilter = dead_kqfilter;
465 if (devsw->d_flags & D_TTY) {
466 if (devsw->d_ioctl == NULL) devsw->d_ioctl = ttyioctl;
467 if (devsw->d_read == NULL) devsw->d_read = ttyread;
468 if (devsw->d_write == NULL) devsw->d_write = ttywrite;
469 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = ttykqfilter;
470 if (devsw->d_poll == NULL) devsw->d_poll = ttypoll;
473 if (devsw->d_flags & D_NEEDGIANT) {
474 if (devsw->d_gianttrick == NULL) {
475 memcpy(dsw2, devsw, sizeof *dsw2);
476 devsw->d_gianttrick = dsw2;
481 #define FIXUP(member, noop, giant) \
483 if (devsw->member == NULL) { \
484 devsw->member = noop; \
485 } else if (devsw->d_flags & D_NEEDGIANT) \
486 devsw->member = giant; \
490 FIXUP(d_open, null_open, giant_open);
491 FIXUP(d_fdopen, NULL, giant_fdopen);
492 FIXUP(d_close, null_close, giant_close);
493 FIXUP(d_read, no_read, giant_read);
494 FIXUP(d_write, no_write, giant_write);
495 FIXUP(d_ioctl, no_ioctl, giant_ioctl);
496 FIXUP(d_poll, no_poll, giant_poll);
497 FIXUP(d_mmap, no_mmap, giant_mmap);
498 FIXUP(d_strategy, no_strategy, giant_strategy);
499 FIXUP(d_kqfilter, no_kqfilter, giant_kqfilter);
501 if (devsw->d_dump == NULL) devsw->d_dump = no_dump;
503 LIST_INIT(&devsw->d_devs);
505 devsw->d_flags |= D_INIT;
511 make_dev_credv(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid,
512 gid_t gid, int mode, const char *fmt, va_list ap)
517 KASSERT((minornr & ~MAXMINOR) == 0,
518 ("Invalid minor (0x%x) in make_dev", minornr));
520 if (!(devsw->d_flags & D_INIT))
524 dev = newdev(devsw, minornr, dev);
525 if (dev->si_flags & SI_CHEAPCLONE &&
526 dev->si_flags & SI_NAMED) {
528 * This is allowed as it removes races and generally
529 * simplifies cloning devices.
535 KASSERT(!(dev->si_flags & SI_NAMED),
536 ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)",
537 devsw->d_name, minor(dev), devtoname(dev)));
539 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
540 if (i > (sizeof dev->__si_namebuf - 1)) {
541 printf("WARNING: Device name truncated! (%s)\n",
545 dev->si_flags |= SI_NAMED;
547 dev->si_cred = crhold(cr);
560 make_dev(struct cdevsw *devsw, int minornr, uid_t uid, gid_t gid, int mode,
561 const char *fmt, ...)
567 dev = make_dev_credv(devsw, minornr, NULL, uid, gid, mode, fmt, ap);
573 make_dev_cred(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid,
574 gid_t gid, int mode, const char *fmt, ...)
580 dev = make_dev_credv(devsw, minornr, cr, uid, gid, mode, fmt, ap);
587 dev_dependsl(struct cdev *pdev, struct cdev *cdev)
590 cdev->si_parent = pdev;
591 cdev->si_flags |= SI_CHILD;
592 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
597 dev_depends(struct cdev *pdev, struct cdev *cdev)
601 dev_dependsl(pdev, cdev);
606 make_dev_alias(struct cdev *pdev, const char *fmt, ...)
614 dev->si_flags |= SI_ALIAS;
615 dev->si_flags |= SI_NAMED;
617 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
618 if (i > (sizeof dev->__si_namebuf - 1)) {
619 printf("WARNING: Device name truncated! (%s)\n",
626 dev_depends(pdev, dev);
631 destroy_devl(struct cdev *dev)
635 mtx_assert(&devmtx, MA_OWNED);
636 KASSERT(dev->si_flags & SI_NAMED,
637 ("WARNING: Driver mistake: destroy_dev on %d\n", minor(dev)));
641 /* Remove name marking */
642 dev->si_flags &= ~SI_NAMED;
644 /* If we are a child, remove us from the parents list */
645 if (dev->si_flags & SI_CHILD) {
646 LIST_REMOVE(dev, si_siblings);
647 dev->si_flags &= ~SI_CHILD;
650 /* Kill our children */
651 while (!LIST_EMPTY(&dev->si_children))
652 destroy_devl(LIST_FIRST(&dev->si_children));
654 /* Remove from clone list */
655 if (dev->si_flags & SI_CLONELIST) {
656 LIST_REMOVE(dev, si_clone);
657 dev->si_flags &= ~SI_CLONELIST;
661 dev->si_devsw = NULL; /* already NULL for SI_ALIAS */
662 while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) {
663 printf("Purging %lu threads from %s\n",
664 dev->si_threadcount, devtoname(dev));
666 msleep(csw, &devmtx, PRIBIO, "devprg", hz/10);
668 if (csw != NULL && csw->d_purge != NULL)
669 printf("All threads purged from %s\n", devtoname(dev));
673 bzero(&dev->__si_u, sizeof(dev->__si_u));
675 if (!(dev->si_flags & SI_ALIAS)) {
676 /* Remove from cdevsw list */
677 LIST_REMOVE(dev, si_list);
679 /* If cdevsw has no more struct cdev *'s, clean it */
680 if (LIST_EMPTY(&csw->d_devs))
683 dev->si_flags &= ~SI_ALIAS;
685 if (dev->si_refcount > 0) {
686 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list);
693 destroy_dev(struct cdev *dev)
702 devtoname(struct cdev *dev)
708 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
710 csw = dev_refthread(dev);
712 sprintf(p, "(%s)", csw->d_name);
717 if (mynor < 0 || mynor > 255)
718 sprintf(p, "/%#x", (u_int)mynor);
720 sprintf(p, "/%d", mynor);
722 return (dev->si_name);
726 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
731 if (bcmp(stem, name, i) != 0)
733 if (!isdigit(name[i]))
736 if (name[i] == '0' && isdigit(name[i+1]))
738 while (isdigit(name[i])) {
740 u += name[i++] - '0';
753 * Helper functions for cloning device drivers.
755 * The objective here is to make it unnecessary for the device drivers to
756 * use rman or similar to manage their unit number space. Due to the way
757 * we do "on-demand" devices, using rman or other "private" methods
758 * will be very tricky to lock down properly once we lock down this file.
760 * Instead we give the drivers these routines which puts the struct cdev *'s
761 * that are to be managed on their own list, and gives the driver the ability
762 * to ask for the first free unit number or a given specified unit number.
764 * In addition these routines support paired devices (pty, nmdm and similar)
765 * by respecting a number of "flag" bits in the minor number.
770 LIST_HEAD(,cdev) head;
774 clone_setup(struct clonedevs **cdp)
777 *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO);
778 LIST_INIT(&(*cdp)->head);
782 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, struct cdev **dp, u_int extra)
784 struct clonedevs *cd;
785 struct cdev *dev, *ndev, *dl, *de;
788 KASSERT(*cdp != NULL,
789 ("clone_setup() not called in driver \"%s\"", csw->d_name));
790 KASSERT(!(extra & CLONE_UNITMASK),
791 ("Illegal extra bits (0x%x) in clone_create", extra));
792 KASSERT(*up <= CLONE_UNITMASK,
793 ("Too high unit (0x%x) in clone_create", *up));
795 if (!(csw->d_flags & D_INIT))
799 * Search the list for a lot of things in one go:
800 * A preexisting match is returned immediately.
801 * The lowest free unit number if we are passed -1, and the place
802 * in the list where we should insert that new element.
803 * The place to insert a specified unit number, if applicable
804 * the end of the list.
807 ndev = devfs_alloc();
812 LIST_FOREACH(dev, &cd->head, si_clone) {
813 KASSERT(dev->si_flags & SI_CLONELIST,
814 ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
816 if (u == (unit | extra)) {
822 if (unit == -1 && u == low) {
826 } else if (u < (unit | extra)) {
829 } else if (u > (unit | extra)) {
835 unit = low & CLONE_UNITMASK;
836 dev = newdev(csw, unit2minor(unit | extra), ndev);
837 if (dev->si_flags & SI_CLONELIST) {
838 printf("dev %p (%s) is on clonelist\n", dev, dev->si_name);
839 printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra);
840 LIST_FOREACH(dev, &cd->head, si_clone) {
841 printf("\t%p %s\n", dev, dev->si_name);
845 KASSERT(!(dev->si_flags & SI_CLONELIST),
846 ("Dev %p(%s) should not be on clonelist", dev, dev->si_name));
848 LIST_INSERT_BEFORE(dl, dev, si_clone);
850 LIST_INSERT_AFTER(de, dev, si_clone);
852 LIST_INSERT_HEAD(&cd->head, dev, si_clone);
853 dev->si_flags |= SI_CLONELIST;
860 * Kill everything still on the list. The driver should already have
861 * disposed of any softc hung of the struct cdev *'s at this time.
864 clone_cleanup(struct clonedevs **cdp)
866 struct cdev *dev, *tdev;
867 struct clonedevs *cd;
873 LIST_FOREACH_SAFE(dev, &cd->head, si_clone, tdev) {
874 KASSERT(dev->si_flags & SI_CLONELIST,
875 ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
876 KASSERT(dev->si_flags & SI_NAMED,
877 ("Driver has goofed in cloning underways udev %x", dev->si_drv0));