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