Home | History | Annotate | Line # | Download | only in putter
putter.c revision 1.35.8.1
      1 /*	$NetBSD: putter.c,v 1.35.8.1 2016/07/18 06:25:40 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006, 2007  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by the
      7  * Ulla Tuominen Foundation and the Finnish Cultural Foundation and the
      8  * Research Foundation of Helsinki University of Technology
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     20  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Pass-to-Userspace TransporTER: generic kernel-user request-response
     34  * transport interface.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: putter.c,v 1.35.8.1 2016/07/18 06:25:40 pgoyette Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/conf.h>
     43 #include <sys/file.h>
     44 #include <sys/filedesc.h>
     45 #include <sys/kmem.h>
     46 #include <sys/poll.h>
     47 #include <sys/stat.h>
     48 #include <sys/socketvar.h>
     49 #include <sys/module.h>
     50 #include <sys/kauth.h>
     51 #include <sys/localcount.h>
     52 
     53 #include <dev/putter/putter_sys.h>
     54 
     55 /*
     56  * Device routines.  These are for when /dev/putter is initially
     57  * opened before it has been cloned.
     58  */
     59 
     60 dev_type_open(puttercdopen);
     61 dev_type_close(puttercdclose);
     62 dev_type_ioctl(puttercdioctl);
     63 
     64 /* dev */
     65 #ifdef _MODULE
     66 struct localcount putter_localcount;
     67 #endif
     68 
     69 const struct cdevsw putter_cdevsw = {
     70 	.d_open = puttercdopen,
     71 	.d_close = puttercdclose,
     72 	.d_read = noread,
     73 	.d_write = nowrite,
     74 	.d_ioctl = noioctl,
     75 	.d_stop = nostop,
     76 	.d_tty = notty,
     77 	.d_poll = nopoll,
     78 	.d_mmap = nommap,
     79 	.d_kqfilter = nokqfilter,
     80 	.d_discard = nodiscard,
     81 #ifdef _MODULE
     82 	.d_localcount = &putter_localcount,
     83 #endif
     84 	.d_flag = D_OTHER
     85 };
     86 
     87 /*
     88  * Configuration data.
     89  *
     90  * This is static-size for now.  Will be redone for devfs.
     91  */
     92 
     93 #define PUTTER_CONFSIZE 16
     94 
     95 static struct putter_config {
     96 	int	pc_minor;
     97 	int	(*pc_config)(int, int, int);
     98 } putterconf[PUTTER_CONFSIZE];
     99 
    100 static int
    101 putter_configure(dev_t dev, int flags, int fmt, int fd)
    102 {
    103 	struct putter_config *pc;
    104 
    105 	/* are we the catch-all node? */
    106 	if (minor(dev) == PUTTER_MINOR_WILDCARD
    107 	    || minor(dev) == PUTTER_MINOR_COMPAT)
    108 		return 0;
    109 
    110 	/* nopes?  try to configure us */
    111 	for (pc = putterconf; pc->pc_config; pc++)
    112 		if (minor(dev) == pc->pc_minor)
    113 			return pc->pc_config(fd, flags, fmt);
    114 	return ENXIO;
    115 }
    116 
    117 int
    118 putter_register(putter_config_fn pcfn, int minor)
    119 {
    120 	int i;
    121 
    122 	for (i = 0; i < PUTTER_CONFSIZE; i++)
    123 		if (putterconf[i].pc_config == NULL)
    124 			break;
    125 	if (i == PUTTER_CONFSIZE)
    126 		return EBUSY;
    127 
    128 	putterconf[i].pc_minor = minor;
    129 	putterconf[i].pc_config = pcfn;
    130 	return 0;
    131 }
    132 
    133 /*
    134  * putter instance structures.  these are always allocated and freed
    135  * from the context of the transport user.
    136  */
    137 struct putter_instance {
    138 	pid_t			pi_pid;
    139 	int			pi_idx;
    140 	int			pi_fd;
    141 	struct selinfo		pi_sel;
    142 
    143 	void			*pi_private;
    144 	struct putter_ops	*pi_pop;
    145 
    146 	uint8_t			*pi_curput;
    147 	size_t			pi_curres;
    148 	void			*pi_curopaq;
    149 	struct timespec		pi_atime;
    150 	struct timespec		pi_mtime;
    151 	struct timespec		pi_btime;
    152 
    153 	TAILQ_ENTRY(putter_instance) pi_entries;
    154 };
    155 #define PUTTER_EMBRYO ((void *)-1)	/* before attach	*/
    156 #define PUTTER_DEAD ((void *)-2)	/* after detach		*/
    157 
    158 static TAILQ_HEAD(, putter_instance) putter_ilist
    159     = TAILQ_HEAD_INITIALIZER(putter_ilist);
    160 
    161 static int get_pi_idx(struct putter_instance *);
    162 
    163 #ifdef DEBUG
    164 #ifndef PUTTERDEBUG
    165 #define PUTTERDEBUG
    166 #endif
    167 #endif
    168 
    169 #ifdef PUTTERDEBUG
    170 int putterdebug = 0;
    171 #define DPRINTF(x) if (putterdebug > 0) printf x
    172 #define DPRINTF_VERBOSE(x) if (putterdebug > 1) printf x
    173 #else
    174 #define DPRINTF(x)
    175 #define DPRINTF_VERBOSE(x)
    176 #endif
    177 
    178 /*
    179  * public init / deinit
    180  */
    181 
    182 /* protects both the list and the contents of the list elements */
    183 static kmutex_t pi_mtx;
    184 
    185 void putterattach(void);
    186 
    187 void
    188 putterattach(void)
    189 {
    190 
    191 	mutex_init(&pi_mtx, MUTEX_DEFAULT, IPL_NONE);
    192 }
    193 
    194 #if 0
    195 void
    196 putter_destroy(void)
    197 {
    198 
    199 	mutex_destroy(&pi_mtx);
    200 }
    201 #endif
    202 
    203 /*
    204  * fd routines, for cloner
    205  */
    206 static int putter_fop_read(file_t *, off_t *, struct uio *,
    207 			   kauth_cred_t, int);
    208 static int putter_fop_write(file_t *, off_t *, struct uio *,
    209 			    kauth_cred_t, int);
    210 static int putter_fop_ioctl(file_t*, u_long, void *);
    211 static int putter_fop_poll(file_t *, int);
    212 static int putter_fop_stat(file_t *, struct stat *);
    213 static int putter_fop_close(file_t *);
    214 static int putter_fop_kqfilter(file_t *, struct knote *);
    215 
    216 
    217 static const struct fileops putter_fileops = {
    218 	.fo_read = putter_fop_read,
    219 	.fo_write = putter_fop_write,
    220 	.fo_ioctl = putter_fop_ioctl,
    221 	.fo_fcntl = fnullop_fcntl,
    222 	.fo_poll = putter_fop_poll,
    223 	.fo_stat = putter_fop_stat,
    224 	.fo_close = putter_fop_close,
    225 	.fo_kqfilter = putter_fop_kqfilter,
    226 	.fo_restart = fnullop_restart,
    227 };
    228 
    229 static int
    230 putter_fop_read(file_t *fp, off_t *off, struct uio *uio,
    231 	kauth_cred_t cred, int flags)
    232 {
    233 	struct putter_instance *pi = fp->f_data;
    234 	size_t origres, moved;
    235 	int error;
    236 
    237 	KERNEL_LOCK(1, NULL);
    238 	getnanotime(&pi->pi_atime);
    239 
    240 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
    241 		printf("putter_fop_read: private %d not inited\n", pi->pi_idx);
    242 		KERNEL_UNLOCK_ONE(NULL);
    243 		return ENOENT;
    244 	}
    245 
    246 	if (pi->pi_curput == NULL) {
    247 		error = pi->pi_pop->pop_getout(pi->pi_private, uio->uio_resid,
    248 		    fp->f_flag & O_NONBLOCK, &pi->pi_curput,
    249 		    &pi->pi_curres, &pi->pi_curopaq);
    250 		if (error) {
    251 			KERNEL_UNLOCK_ONE(NULL);
    252 			return error;
    253 		}
    254 	}
    255 
    256 	origres = uio->uio_resid;
    257 	error = uiomove(pi->pi_curput, pi->pi_curres, uio);
    258 	moved = origres - uio->uio_resid;
    259 	DPRINTF(("putter_fop_read (%p): moved %zu bytes from %p, error %d\n",
    260 	    pi, moved, pi->pi_curput, error));
    261 
    262 	KASSERT(pi->pi_curres >= moved);
    263 	pi->pi_curres -= moved;
    264 	pi->pi_curput += moved;
    265 
    266 	if (pi->pi_curres == 0) {
    267 		pi->pi_pop->pop_releaseout(pi->pi_private,
    268 		    pi->pi_curopaq, error);
    269 		pi->pi_curput = NULL;
    270 	}
    271 
    272 	KERNEL_UNLOCK_ONE(NULL);
    273 	return error;
    274 }
    275 
    276 static int
    277 putter_fop_write(file_t *fp, off_t *off, struct uio *uio,
    278 	kauth_cred_t cred, int flags)
    279 {
    280 	struct putter_instance *pi = fp->f_data;
    281 	struct putter_hdr pth;
    282 	uint8_t *buf;
    283 	size_t frsize;
    284 	int error;
    285 
    286 	KERNEL_LOCK(1, NULL);
    287 	getnanotime(&pi->pi_mtime);
    288 
    289 	DPRINTF(("putter_fop_write (%p): writing response, resid %zu\n",
    290 	    pi->pi_private, uio->uio_resid));
    291 
    292 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
    293 		printf("putter_fop_write: putter %d not inited\n", pi->pi_idx);
    294 		KERNEL_UNLOCK_ONE(NULL);
    295 		return ENOENT;
    296 	}
    297 
    298 	error = uiomove(&pth, sizeof(struct putter_hdr), uio);
    299 	if (error) {
    300 		KERNEL_UNLOCK_ONE(NULL);
    301 		return error;
    302 	}
    303 
    304 	/* Sorry mate, the kernel doesn't buffer. */
    305 	frsize = pth.pth_framelen - sizeof(struct putter_hdr);
    306 	if (uio->uio_resid < frsize) {
    307 		KERNEL_UNLOCK_ONE(NULL);
    308 		return EINVAL;
    309 	}
    310 
    311 	buf = kmem_alloc(frsize + sizeof(struct putter_hdr), KM_SLEEP);
    312 	memcpy(buf, &pth, sizeof(pth));
    313 	error = uiomove(buf+sizeof(struct putter_hdr), frsize, uio);
    314 	if (error == 0) {
    315 		pi->pi_pop->pop_dispatch(pi->pi_private,
    316 		    (struct putter_hdr *)buf);
    317 	}
    318 	kmem_free(buf, frsize + sizeof(struct putter_hdr));
    319 
    320 	KERNEL_UNLOCK_ONE(NULL);
    321 	return error;
    322 }
    323 
    324 /*
    325  * Poll query interface.  The question is only if an event
    326  * can be read from us.
    327  */
    328 #define PUTTERPOLL_EVSET (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)
    329 static int
    330 putter_fop_poll(file_t *fp, int events)
    331 {
    332 	struct putter_instance *pi = fp->f_data;
    333 	int revents;
    334 
    335 	KERNEL_LOCK(1, NULL);
    336 
    337 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
    338 		printf("putter_fop_ioctl: putter %d not inited\n", pi->pi_idx);
    339 		KERNEL_UNLOCK_ONE(NULL);
    340 		return ENOENT;
    341 	}
    342 
    343 	revents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
    344 	if ((events & PUTTERPOLL_EVSET) == 0) {
    345 		KERNEL_UNLOCK_ONE(NULL);
    346 		return revents;
    347 	}
    348 
    349 	/* check queue */
    350 	if (pi->pi_pop->pop_waitcount(pi->pi_private))
    351 		revents |= PUTTERPOLL_EVSET;
    352 	else
    353 		selrecord(curlwp, &pi->pi_sel);
    354 
    355 	KERNEL_UNLOCK_ONE(NULL);
    356 	return revents;
    357 }
    358 
    359 /*
    360  * device close = forced unmount.
    361  *
    362  * unmounting is a frightfully complex operation to avoid races
    363  */
    364 static int
    365 putter_fop_close(file_t *fp)
    366 {
    367 	struct putter_instance *pi = fp->f_data;
    368 	int rv;
    369 
    370 	DPRINTF(("putter_fop_close: device closed\n"));
    371 
    372 	KERNEL_LOCK(1, NULL);
    373 
    374  restart:
    375 	mutex_enter(&pi_mtx);
    376 	/*
    377 	 * First check if the driver was never born.  In that case
    378 	 * remove the instance from the list.  If mount is attempted later,
    379 	 * it will simply fail.
    380 	 */
    381 	if (pi->pi_private == PUTTER_EMBRYO) {
    382 		TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
    383 		mutex_exit(&pi_mtx);
    384 
    385 		DPRINTF(("putter_fop_close: data associated with fp %p was "
    386 		    "embryonic\n", fp));
    387 
    388 		goto out;
    389 	}
    390 
    391 	/*
    392 	 * Next, analyze if unmount was called and the instance is dead.
    393 	 * In this case we can just free the structure and go home, it
    394 	 * was removed from the list by putter_rmprivate().
    395 	 */
    396 	if (pi->pi_private == PUTTER_DEAD) {
    397 		mutex_exit(&pi_mtx);
    398 
    399 		DPRINTF(("putter_fop_close: putter associated with fp %p (%d) "
    400 		    "dead, freeing\n", fp, pi->pi_idx));
    401 
    402 		goto out;
    403 	}
    404 
    405 	/*
    406 	 * So we have a reference.  Proceed to unravel the
    407 	 * underlying driver.
    408 	 */
    409 	mutex_exit(&pi_mtx);
    410 
    411 	/* hmm?  suspicious locking? */
    412 	if (pi->pi_curput != NULL) {
    413 		pi->pi_pop->pop_releaseout(pi->pi_private, pi->pi_curopaq,
    414 		    ENXIO);
    415 		pi->pi_curput = NULL;
    416 	}
    417 	while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART)
    418 		goto restart;
    419 
    420  out:
    421 	KERNEL_UNLOCK_ONE(NULL);
    422 	/*
    423 	 * Finally, release the instance information.  It was already
    424 	 * removed from the list by putter_rmprivate() and we know it's
    425 	 * dead, so no need to lock.
    426 	 */
    427 	kmem_free(pi, sizeof(struct putter_instance));
    428 
    429 	return 0;
    430 }
    431 
    432 static int
    433 putter_fop_stat(file_t *fp, struct stat *st)
    434 {
    435 	struct putter_instance *pi = fp->f_data;
    436 
    437 	(void)memset(st, 0, sizeof(*st));
    438 	KERNEL_LOCK(1, NULL);
    439 	st->st_dev = makedev(cdevsw_lookup_major(&putter_cdevsw), pi->pi_idx);
    440 	st->st_atimespec = pi->pi_atime;
    441 	st->st_mtimespec = pi->pi_mtime;
    442 	st->st_ctimespec = st->st_birthtimespec = pi->pi_btime;
    443 	st->st_uid = kauth_cred_geteuid(fp->f_cred);
    444 	st->st_gid = kauth_cred_getegid(fp->f_cred);
    445 	st->st_mode = S_IFCHR;
    446 	KERNEL_UNLOCK_ONE(NULL);
    447 	return 0;
    448 }
    449 
    450 static int
    451 putter_fop_ioctl(file_t *fp, u_long cmd, void *data)
    452 {
    453 
    454 	/*
    455 	 * work already done in sys_ioctl().  skip sanity checks to enable
    456 	 * setting non-blocking fd on an embryotic driver.
    457 	 */
    458 	if (cmd == FIONBIO)
    459 		return 0;
    460 
    461 	return EINVAL;
    462 }
    463 
    464 /* kqueue stuff */
    465 
    466 static void
    467 filt_putterdetach(struct knote *kn)
    468 {
    469 	struct putter_instance *pi = kn->kn_hook;
    470 
    471 	KERNEL_LOCK(1, NULL);
    472 	mutex_enter(&pi_mtx);
    473 	SLIST_REMOVE(&pi->pi_sel.sel_klist, kn, knote, kn_selnext);
    474 	mutex_exit(&pi_mtx);
    475 	KERNEL_UNLOCK_ONE(NULL);
    476 }
    477 
    478 static int
    479 filt_putter(struct knote *kn, long hint)
    480 {
    481 	struct putter_instance *pi = kn->kn_hook;
    482 	int error, rv;
    483 
    484 	KERNEL_LOCK(1, NULL);
    485 	error = 0;
    486 	mutex_enter(&pi_mtx);
    487 	if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD)
    488 		error = 1;
    489 	mutex_exit(&pi_mtx);
    490 	if (error) {
    491 		KERNEL_UNLOCK_ONE(NULL);
    492 		return 0;
    493 	}
    494 
    495 	kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private);
    496 	rv = kn->kn_data != 0;
    497 	KERNEL_UNLOCK_ONE(NULL);
    498 	return rv;
    499 }
    500 
    501 static const struct filterops putter_filtops =
    502 	{ 1, NULL, filt_putterdetach, filt_putter };
    503 
    504 static int
    505 putter_fop_kqfilter(file_t *fp, struct knote *kn)
    506 {
    507 	struct putter_instance *pi = fp->f_data;
    508 	struct klist *klist;
    509 
    510 	KERNEL_LOCK(1, NULL);
    511 
    512 	switch (kn->kn_filter) {
    513 	case EVFILT_READ:
    514 		klist = &pi->pi_sel.sel_klist;
    515 		kn->kn_fop = &putter_filtops;
    516 		kn->kn_hook = pi;
    517 
    518 		mutex_enter(&pi_mtx);
    519 		SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    520 		mutex_exit(&pi_mtx);
    521 
    522 		break;
    523 	case EVFILT_WRITE:
    524 		kn->kn_fop = &seltrue_filtops;
    525 		break;
    526 	default:
    527 		KERNEL_UNLOCK_ONE(NULL);
    528 		return EINVAL;
    529 	}
    530 
    531 	KERNEL_UNLOCK_ONE(NULL);
    532 	return 0;
    533 }
    534 
    535 int
    536 puttercdopen(dev_t dev, int flags, int fmt, struct lwp *l)
    537 {
    538 	struct putter_instance *pi;
    539 	file_t *fp;
    540 	int error, fd, idx;
    541 	proc_t *p;
    542 
    543 	p = curproc;
    544 	pi = kmem_alloc(sizeof(struct putter_instance), KM_SLEEP);
    545 	mutex_enter(&pi_mtx);
    546 	idx = get_pi_idx(pi);
    547 
    548 	pi->pi_pid = p->p_pid;
    549 	pi->pi_idx = idx;
    550 	pi->pi_curput = NULL;
    551 	pi->pi_curres = 0;
    552 	pi->pi_curopaq = NULL;
    553 	getnanotime(&pi->pi_btime);
    554 	pi->pi_atime = pi->pi_mtime = pi->pi_btime;
    555 	selinit(&pi->pi_sel);
    556 	mutex_exit(&pi_mtx);
    557 
    558 	if ((error = fd_allocfile(&fp, &fd)) != 0)
    559 		goto bad1;
    560 
    561 	if ((error = putter_configure(dev, flags, fmt, fd)) != 0)
    562 		goto bad2;
    563 
    564 	DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n",
    565 	    pi->pi_pid));
    566 
    567 	error = fd_clone(fp, fd, FREAD|FWRITE, &putter_fileops, pi);
    568 	KASSERT(error == EMOVEFD);
    569 	return error;
    570 
    571  bad2:
    572  	fd_abort(p, fp, fd);
    573  bad1:
    574 	putter_detach(pi);
    575 	kmem_free(pi, sizeof(struct putter_instance));
    576 	return error;
    577 }
    578 
    579 int
    580 puttercdclose(dev_t dev, int flags, int fmt, struct lwp *l)
    581 {
    582 
    583 	panic("puttercdclose impossible\n");
    584 
    585 	return 0;
    586 }
    587 
    588 
    589 /*
    590  * Set the private structure for the file descriptor.  This is
    591  * typically done immediately when the counterpart has knowledge
    592  * about the private structure's address and the file descriptor
    593  * (e.g. vfs mount routine).
    594  *
    595  * We only want to make sure that the caller had the right to open the
    596  * device, we don't so much care about which context it gets in case
    597  * the same process opened multiple (since they are equal at this point).
    598  */
    599 struct putter_instance *
    600 putter_attach(pid_t pid, int fd, void *ppriv, struct putter_ops *pop)
    601 {
    602 	struct putter_instance *pi = NULL;
    603 
    604 	mutex_enter(&pi_mtx);
    605 	TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
    606 		if (pi->pi_pid == pid && pi->pi_private == PUTTER_EMBRYO) {
    607 			pi->pi_private = ppriv;
    608 			pi->pi_fd = fd;
    609 			pi->pi_pop = pop;
    610 			break;
    611 		    }
    612 	}
    613 	mutex_exit(&pi_mtx);
    614 
    615 	DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi,
    616 	    pi ? pi->pi_pid : 0, pi ? pi->pi_fd : 0));
    617 
    618 	return pi;
    619 }
    620 
    621 /*
    622  * Remove fp <-> private mapping.
    623  */
    624 void
    625 putter_detach(struct putter_instance *pi)
    626 {
    627 
    628 	mutex_enter(&pi_mtx);
    629 	TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
    630 	pi->pi_private = PUTTER_DEAD;
    631 	mutex_exit(&pi_mtx);
    632 	seldestroy(&pi->pi_sel);
    633 
    634 	DPRINTF(("putter_nukebypmp: nuked %p\n", pi));
    635 }
    636 
    637 void
    638 putter_notify(struct putter_instance *pi)
    639 {
    640 
    641 	selnotify(&pi->pi_sel, 0, 0);
    642 }
    643 
    644 /* search sorted list of instances for free minor, sorted insert arg */
    645 static int
    646 get_pi_idx(struct putter_instance *pi_i)
    647 {
    648 	struct putter_instance *pi;
    649 	int i;
    650 
    651 	KASSERT(mutex_owned(&pi_mtx));
    652 
    653 	i = 0;
    654 	TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
    655 		if (i != pi->pi_idx)
    656 			break;
    657 		i++;
    658 	}
    659 
    660 	pi_i->pi_private = PUTTER_EMBRYO;
    661 
    662 	if (pi == NULL)
    663 		TAILQ_INSERT_TAIL(&putter_ilist, pi_i, pi_entries);
    664 	else
    665 		TAILQ_INSERT_BEFORE(pi, pi_i, pi_entries);
    666 
    667 	return i;
    668 }
    669 
    670 MODULE(MODULE_CLASS_DRIVER, putter, NULL);
    671 
    672 static int
    673 putter_modcmd(modcmd_t cmd, void *arg)
    674 {
    675 #ifdef _MODULE
    676 	devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
    677 
    678 	switch (cmd) {
    679 	case MODULE_CMD_INIT:
    680 		putterattach();
    681 		return devsw_attach("putter", NULL, &bmajor,
    682 		    &putter_cdevsw, &cmajor);
    683 	case MODULE_CMD_FINI:
    684 		return ENOTTY; /* XXX: putterdetach */
    685 	default:
    686 		return ENOTTY;
    687 	}
    688 #else
    689 	if (cmd == MODULE_CMD_INIT)
    690 		return 0;
    691 	return ENOTTY;
    692 #endif
    693 }
    694