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