Home | History | Annotate | Line # | Download | only in wscons
wsmux.c revision 1.60
      1 /*	$NetBSD: wsmux.c,v 1.60 2015/08/24 22:50:33 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Lennart Augustsson <lennart (at) augustsson.net>
      8  *         Carlstedt Research & 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * wscons mux device.
     34  *
     35  * The mux device is a collection of real mice and keyboards and acts as
     36  * a merge point for all the events from the different real devices.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: wsmux.c,v 1.60 2015/08/24 22:50:33 pooka Exp $");
     41 
     42 #ifdef _KERNEL_OPT
     43 #include "opt_compat_netbsd.h"
     44 #include "opt_modular.h"
     45 #endif
     46 
     47 #include "wsdisplay.h"
     48 #include "wsmux.h"
     49 #include "wskbd.h"
     50 #include "wsmouse.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/conf.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/poll.h>
     56 #include <sys/fcntl.h>
     57 #include <sys/kernel.h>
     58 #include <sys/malloc.h>
     59 #include <sys/proc.h>
     60 #include <sys/queue.h>
     61 #include <sys/syslog.h>
     62 #include <sys/systm.h>
     63 #include <sys/tty.h>
     64 #include <sys/signalvar.h>
     65 #include <sys/device.h>
     66 
     67 #include "opt_wsdisplay_compat.h"
     68 
     69 #include <dev/wscons/wsconsio.h>
     70 #include <dev/wscons/wsksymdef.h>
     71 #include <dev/wscons/wseventvar.h>
     72 #include <dev/wscons/wscons_callbacks.h>
     73 #include <dev/wscons/wsmuxvar.h>
     74 
     75 #include "ioconf.h"
     76 
     77 #ifdef WSMUX_DEBUG
     78 #define DPRINTF(x)	if (wsmuxdebug) printf x
     79 #define DPRINTFN(n,x)	if (wsmuxdebug > (n)) printf x
     80 int	wsmuxdebug = 0;
     81 #else
     82 #define DPRINTF(x)
     83 #define DPRINTFN(n,x)
     84 #endif
     85 
     86 /*
     87  * The wsmux pseudo device is used to multiplex events from several wsmouse,
     88  * wskbd, and/or wsmux devices together.
     89  * The devices connected together form a tree with muxes in the interior
     90  * and real devices (mouse and kbd) at the leaves.  The special case of
     91  * a tree with one node (mux or other) is supported as well.
     92  * Only the device at the root of the tree can be opened (if a non-root
     93  * device is opened the subtree rooted at that point is severed from the
     94  * containing tree).  When the root is opened it allocates a wseventvar
     95  * struct which all the nodes in the tree will send their events too.
     96  * An ioctl() performed on the root is propagated to all the nodes.
     97  * There are also ioctl() operations to add and remove nodes from a tree.
     98  */
     99 
    100 static int wsmux_mux_open(struct wsevsrc *, struct wseventvar *);
    101 static int wsmux_mux_close(struct wsevsrc *);
    102 
    103 static void wsmux_do_open(struct wsmux_softc *, struct wseventvar *);
    104 
    105 static void wsmux_do_close(struct wsmux_softc *);
    106 #if NWSDISPLAY > 0
    107 static int wsmux_evsrc_set_display(device_t, struct wsevsrc *);
    108 #else
    109 #define wsmux_evsrc_set_display NULL
    110 #endif
    111 
    112 static int wsmux_do_displayioctl(device_t dev, u_long cmd,
    113 				 void *data, int flag, struct lwp *l);
    114 static int wsmux_do_ioctl(device_t, u_long, void *,int,struct lwp *);
    115 
    116 static int wsmux_add_mux(int, struct wsmux_softc *);
    117 
    118 #define WSMUXDEV(n) ((n) & 0x7f)
    119 #define WSMUXCTL(n) ((n) & 0x80)
    120 
    121 dev_type_open(wsmuxopen);
    122 dev_type_close(wsmuxclose);
    123 dev_type_read(wsmuxread);
    124 dev_type_ioctl(wsmuxioctl);
    125 dev_type_poll(wsmuxpoll);
    126 dev_type_kqfilter(wsmuxkqfilter);
    127 
    128 const struct cdevsw wsmux_cdevsw = {
    129 	.d_open = wsmuxopen,
    130 	.d_close = wsmuxclose,
    131 	.d_read = wsmuxread,
    132 	.d_write = nowrite,
    133 	.d_ioctl = wsmuxioctl,
    134 	.d_stop = nostop,
    135 	.d_tty = notty,
    136 	.d_poll = wsmuxpoll,
    137 	.d_mmap = nommap,
    138 	.d_kqfilter = wsmuxkqfilter,
    139 	.d_discard = nodiscard,
    140 	.d_flag = D_OTHER
    141 };
    142 
    143 struct wssrcops wsmux_srcops = {
    144 	WSMUX_MUX,
    145 	wsmux_mux_open, wsmux_mux_close, wsmux_do_ioctl, wsmux_do_displayioctl,
    146 	wsmux_evsrc_set_display
    147 };
    148 
    149 /* From upper level */
    150 void
    151 wsmuxattach(int n)
    152 {
    153 }
    154 
    155 /* Keep track of all muxes that have been allocated */
    156 static struct wsmux_softc **wsmuxdevs = NULL;
    157 static int nwsmux = 0;
    158 
    159 /* Return mux n, create if necessary */
    160 struct wsmux_softc *
    161 wsmux_getmux(int n)
    162 {
    163 	struct wsmux_softc *sc;
    164 
    165 	n = WSMUXDEV(n);	/* limit range */
    166 
    167 	/* Make sure there is room for mux n in the table */
    168 	if (n >= nwsmux) {
    169 		void *new;
    170 
    171 		new = realloc(wsmuxdevs, (n + 1) * sizeof(*wsmuxdevs),
    172 		    M_DEVBUF, M_ZERO | M_NOWAIT);
    173 		if (new == NULL) {
    174 			printf("wsmux_getmux: no memory for mux %d\n", n);
    175 			return NULL;
    176 		}
    177 		wsmuxdevs = new;
    178 		nwsmux = n + 1;
    179 	}
    180 
    181 	sc = wsmuxdevs[n];
    182 	if (sc == NULL) {
    183 		sc = wsmux_create("wsmux", n);
    184 		if (sc == NULL)
    185 			printf("wsmux: attach out of memory\n");
    186 		wsmuxdevs[n] = sc;
    187 	}
    188 	return (sc);
    189 }
    190 
    191 /*
    192  * open() of the pseudo device from device table.
    193  */
    194 int
    195 wsmuxopen(dev_t dev, int flags, int mode, struct lwp *l)
    196 {
    197 	struct wsmux_softc *sc;
    198 	struct wseventvar *evar;
    199 	int minr, unit;
    200 
    201 	minr = minor(dev);
    202 	unit = WSMUXDEV(minr);
    203 	sc = wsmux_getmux(unit);
    204 	if (sc == NULL)
    205 		return (ENXIO);
    206 
    207 	DPRINTF(("wsmuxopen: %s: sc=%p l=%p\n",
    208 		 device_xname(sc->sc_base.me_dv), sc, l));
    209 
    210 	if (WSMUXCTL(minr)) {
    211 		/* This is the control device which does not allow reads. */
    212 		if (flags & FREAD)
    213 			return (EINVAL);
    214 		return (0);
    215 	}
    216 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    217 		/* Allow write only open */
    218 		return (0);
    219 
    220 	if (sc->sc_base.me_parent != NULL) {
    221 		/* Grab the mux out of the greedy hands of the parent mux. */
    222 		DPRINTF(("wsmuxopen: detach\n"));
    223 		wsmux_detach_sc(&sc->sc_base);
    224 	}
    225 
    226 	if (sc->sc_base.me_evp != NULL)
    227 		/* Already open. */
    228 		return (EBUSY);
    229 
    230 	evar = &sc->sc_base.me_evar;
    231 	wsevent_init(evar, l->l_proc);
    232 #ifdef WSDISPLAY_COMPAT_RAWKBD
    233 	sc->sc_rawkbd = 0;
    234 #endif
    235 
    236 	wsmux_do_open(sc, evar);
    237 
    238 	return (0);
    239 }
    240 
    241 /*
    242  * Open of a mux via the parent mux.
    243  */
    244 int
    245 wsmux_mux_open(struct wsevsrc *me, struct wseventvar *evar)
    246 {
    247 	struct wsmux_softc *sc = (struct wsmux_softc *)me;
    248 
    249 #ifdef DIAGNOSTIC
    250 	if (sc->sc_base.me_evp != NULL) {
    251 		printf("wsmux_mux_open: busy\n");
    252 		return (EBUSY);
    253 	}
    254 	if (sc->sc_base.me_parent == NULL) {
    255 		printf("wsmux_mux_open: no parent\n");
    256 		return (EINVAL);
    257 	}
    258 #endif
    259 
    260 	wsmux_do_open(sc, evar);
    261 
    262 	return (0);
    263 }
    264 
    265 /* Common part of opening a mux. */
    266 void
    267 wsmux_do_open(struct wsmux_softc *sc, struct wseventvar *evar)
    268 {
    269 	struct wsevsrc *me;
    270 
    271 	sc->sc_base.me_evp = evar; /* remember event variable, mark as open */
    272 
    273 	/* Open all children. */
    274 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    275 		DPRINTF(("wsmuxopen: %s: m=%p dev=%s\n",
    276 			 device_xname(sc->sc_base.me_dv), me,
    277 			 device_xname(me->me_dv)));
    278 #ifdef DIAGNOSTIC
    279 		if (me->me_evp != NULL) {
    280 			printf("wsmuxopen: dev already in use\n");
    281 			continue;
    282 		}
    283 		if (me->me_parent != sc) {
    284 			printf("wsmux_do_open: bad child=%p\n", me);
    285 			continue;
    286 		}
    287 		{
    288 		int error = wsevsrc_open(me, evar);
    289 		if (error) {
    290 			DPRINTF(("wsmuxopen: open failed %d\n", error));
    291 		}
    292 		}
    293 #else
    294 		/* ignore errors, failing children will not be marked open */
    295 		(void)wsevsrc_open(me, evar);
    296 #endif
    297 	}
    298 }
    299 
    300 /*
    301  * close() of the pseudo device from device table.
    302  */
    303 int
    304 wsmuxclose(dev_t dev, int flags, int mode,
    305     struct lwp *l)
    306 {
    307 	int minr = minor(dev);
    308 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    309 	struct wseventvar *evar = sc->sc_base.me_evp;
    310 
    311 	if (WSMUXCTL(minr))
    312 		/* control device */
    313 		return (0);
    314 	if (evar == NULL)
    315 		/* Not open for read */
    316 		return (0);
    317 
    318 	wsmux_do_close(sc);
    319 	sc->sc_base.me_evp = NULL;
    320 	wsevent_fini(evar);
    321 	return (0);
    322 }
    323 
    324 /*
    325  * Close of a mux via the parent mux.
    326  */
    327 int
    328 wsmux_mux_close(struct wsevsrc *me)
    329 {
    330 	me->me_evp = NULL;
    331 	wsmux_do_close((struct wsmux_softc *)me);
    332 	return (0);
    333 }
    334 
    335 /* Common part of closing a mux. */
    336 void
    337 wsmux_do_close(struct wsmux_softc *sc)
    338 {
    339 	struct wsevsrc *me;
    340 
    341 	DPRINTF(("wsmuxclose: %s: sc=%p\n",
    342 		 device_xname(sc->sc_base.me_dv), sc));
    343 
    344 	/* Close all the children. */
    345 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    346 		DPRINTF(("wsmuxclose %s: m=%p dev=%s\n",
    347 			 device_xname(sc->sc_base.me_dv), me,
    348 			 device_xname(me->me_dv)));
    349 #ifdef DIAGNOSTIC
    350 		if (me->me_parent != sc) {
    351 			printf("wsmuxclose: bad child=%p\n", me);
    352 			continue;
    353 		}
    354 #endif
    355 		(void)wsevsrc_close(me);
    356 		me->me_evp = NULL;
    357 	}
    358 }
    359 
    360 /*
    361  * read() of the pseudo device from device table.
    362  */
    363 int
    364 wsmuxread(dev_t dev, struct uio *uio, int flags)
    365 {
    366 	int minr = minor(dev);
    367 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    368 	struct wseventvar *evar;
    369 	int error;
    370 
    371 	if (WSMUXCTL(minr)) {
    372 		/* control device */
    373 		return (EINVAL);
    374 	}
    375 
    376 	evar = sc->sc_base.me_evp;
    377 	if (evar == NULL) {
    378 #ifdef DIAGNOSTIC
    379 		/* XXX can we get here? */
    380 		printf("wsmuxread: not open\n");
    381 #endif
    382 		return (EINVAL);
    383 	}
    384 
    385 	DPRINTFN(5,("wsmuxread: %s event read evar=%p\n",
    386 		    device_xname(sc->sc_base.me_dv), evar));
    387 	error = wsevent_read(evar, uio, flags);
    388 	DPRINTFN(5,("wsmuxread: %s event read ==> error=%d\n",
    389 		    device_xname(sc->sc_base.me_dv), error));
    390 	return (error);
    391 }
    392 
    393 /*
    394  * ioctl of the pseudo device from device table.
    395  */
    396 int
    397 wsmuxioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    398 {
    399 	int u = WSMUXDEV(minor(dev));
    400 
    401 	return wsmux_do_ioctl(wsmuxdevs[u]->sc_base.me_dv, cmd, data, flag, l);
    402 }
    403 
    404 /*
    405  * ioctl of a mux via the parent mux, continuation of wsmuxioctl().
    406  */
    407 int
    408 wsmux_do_ioctl(device_t dv, u_long cmd, void *data, int flag,
    409 	       struct lwp *lwp)
    410 {
    411 	struct wsmux_softc *sc = device_private(dv);
    412 	struct wsevsrc *me;
    413 	int error, ok;
    414 	int s, n;
    415 	struct wseventvar *evar;
    416 	struct wscons_event event;
    417 	struct wsmux_device_list *l;
    418 
    419 	DPRINTF(("wsmux_do_ioctl: %s: enter sc=%p, cmd=%08lx\n",
    420 		 device_xname(sc->sc_base.me_dv), sc, cmd));
    421 
    422 	switch (cmd) {
    423 #if defined(COMPAT_50) || defined(MODULAR)
    424 	case WSMUXIO_OINJECTEVENT:
    425 #endif /* defined(COMPAT_50) || defined(MODULAR) */
    426 	case WSMUXIO_INJECTEVENT:
    427 		/* Inject an event, e.g., from moused. */
    428 		DPRINTF(("%s: inject\n", device_xname(sc->sc_base.me_dv)));
    429 
    430 		evar = sc->sc_base.me_evp;
    431 		if (evar == NULL) {
    432 			/* No event sink, so ignore it. */
    433 			DPRINTF(("wsmux_do_ioctl: event ignored\n"));
    434 			return (0);
    435 		}
    436 
    437 		s = spltty();
    438 		event.type = ((struct wscons_event *)data)->type;
    439 		event.value = ((struct wscons_event *)data)->value;
    440 		error = wsevent_inject(evar, &event, 1);
    441 		splx(s);
    442 
    443 		return error;
    444 	case WSMUXIO_ADD_DEVICE:
    445 #define d ((struct wsmux_device *)data)
    446 		DPRINTF(("%s: add type=%d, no=%d\n",
    447 			 device_xname(sc->sc_base.me_dv), d->type, d->idx));
    448 		switch (d->type) {
    449 #if NWSMOUSE > 0
    450 		case WSMUX_MOUSE:
    451 			return (wsmouse_add_mux(d->idx, sc));
    452 #endif
    453 #if NWSKBD > 0
    454 		case WSMUX_KBD:
    455 			return (wskbd_add_mux(d->idx, sc));
    456 #endif
    457 		case WSMUX_MUX:
    458 			return (wsmux_add_mux(d->idx, sc));
    459 		default:
    460 			return (EINVAL);
    461 		}
    462 	case WSMUXIO_REMOVE_DEVICE:
    463 		DPRINTF(("%s: rem type=%d, no=%d\n",
    464 			 device_xname(sc->sc_base.me_dv), d->type, d->idx));
    465 		/* Locate the device */
    466 		TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    467 			if (me->me_ops->type == d->type &&
    468 			    device_unit(me->me_dv) == d->idx) {
    469 				DPRINTF(("wsmux_do_ioctl: detach\n"));
    470 				wsmux_detach_sc(me);
    471 				return (0);
    472 			}
    473 		}
    474 		return (EINVAL);
    475 #undef d
    476 
    477 	case WSMUXIO_LIST_DEVICES:
    478 		DPRINTF(("%s: list\n", device_xname(sc->sc_base.me_dv)));
    479 		l = (struct wsmux_device_list *)data;
    480 		n = 0;
    481 		TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    482 			if (n >= WSMUX_MAXDEV)
    483 				break;
    484 			l->devices[n].type = me->me_ops->type;
    485 			l->devices[n].idx = device_unit(me->me_dv);
    486 			n++;
    487 		}
    488 		l->ndevices = n;
    489 		return (0);
    490 #ifdef WSDISPLAY_COMPAT_RAWKBD
    491 	case WSKBDIO_SETMODE:
    492 		sc->sc_rawkbd = *(int *)data;
    493 		DPRINTF(("wsmux_do_ioctl: save rawkbd = %d\n", sc->sc_rawkbd));
    494 		break;
    495 #endif
    496 
    497 	case WSKBDIO_SETVERSION:
    498 	case WSMOUSEIO_SETVERSION:
    499 	case WSDISPLAYIO_SETVERSION:
    500 		DPRINTF(("%s: WSxxxIO_SETVERSION\n", device_xname(sc->sc_base.me_dv)));
    501 		evar = sc->sc_base.me_evp;
    502 		if (evar == NULL)
    503 			return (EINVAL);
    504 		return wsevent_setversion(evar, *(int *)data);
    505 
    506 	case FIONBIO:
    507 		DPRINTF(("%s: FIONBIO\n", device_xname(sc->sc_base.me_dv)));
    508 		return (0);
    509 
    510 	case FIOASYNC:
    511 		DPRINTF(("%s: FIOASYNC\n", device_xname(sc->sc_base.me_dv)));
    512 		evar = sc->sc_base.me_evp;
    513 		if (evar == NULL)
    514 			return (EINVAL);
    515 		evar->async = *(int *)data != 0;
    516 		return (0);
    517 	case FIOSETOWN:
    518 		DPRINTF(("%s: FIOSETOWN\n", device_xname(sc->sc_base.me_dv)));
    519 		evar = sc->sc_base.me_evp;
    520 		if (evar == NULL)
    521 			return (EINVAL);
    522 		if (-*(int *)data != evar->io->p_pgid
    523 		    && *(int *)data != evar->io->p_pid)
    524 			return (EPERM);
    525 		return (0);
    526 	case TIOCSPGRP:
    527 		DPRINTF(("%s: TIOCSPGRP\n", device_xname(sc->sc_base.me_dv)));
    528 		evar = sc->sc_base.me_evp;
    529 		if (evar == NULL)
    530 			return (EINVAL);
    531 		if (*(int *)data != evar->io->p_pgid)
    532 			return (EPERM);
    533 		return (0);
    534 	default:
    535 		DPRINTF(("%s: unknown\n", device_xname(sc->sc_base.me_dv)));
    536 		break;
    537 	}
    538 
    539 	if (sc->sc_base.me_evp == NULL
    540 #if NWSDISPLAY > 0
    541 	    && sc->sc_base.me_dispdv == NULL
    542 #endif
    543 	    )
    544 		return (EACCES);
    545 
    546 	/* Return 0 if any of the ioctl() succeeds, otherwise the last error */
    547 	error = 0;
    548 	ok = 0;
    549 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    550 #ifdef DIAGNOSTIC
    551 		/* XXX check evp? */
    552 		if (me->me_parent != sc) {
    553 			printf("wsmux_do_ioctl: bad child %p\n", me);
    554 			continue;
    555 		}
    556 #endif
    557 		error = wsevsrc_ioctl(me, cmd, data, flag, lwp);
    558 		DPRINTF(("wsmux_do_ioctl: %s: me=%p dev=%s ==> %d\n",
    559 			 device_xname(sc->sc_base.me_dv), me,
    560 			 device_xname(me->me_dv), error));
    561 		if (!error)
    562 			ok = 1;
    563 	}
    564 	if (ok) {
    565 		error = 0;
    566 		if (cmd == WSKBDIO_SETENCODING) {
    567 			sc->sc_kbd_layout = *((kbd_t *)data);
    568 		}
    569 
    570 	}
    571 
    572 	return (error);
    573 }
    574 
    575 /*
    576  * poll() of the pseudo device from device table.
    577  */
    578 int
    579 wsmuxpoll(dev_t dev, int events, struct lwp *l)
    580 {
    581 	int minr = minor(dev);
    582 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    583 
    584 	if (WSMUXCTL(minr)) {
    585 		/* control device */
    586 		return (0);
    587 	}
    588 
    589 	if (sc->sc_base.me_evp == NULL) {
    590 #ifdef DIAGNOSTIC
    591 		printf("wsmuxpoll: not open\n");
    592 #endif
    593 		return (POLLHUP);
    594 	}
    595 
    596 	return (wsevent_poll(sc->sc_base.me_evp, events, l));
    597 }
    598 
    599 /*
    600  * kqfilter() of the pseudo device from device table.
    601  */
    602 int
    603 wsmuxkqfilter(dev_t dev, struct knote *kn)
    604 {
    605 	int minr = minor(dev);
    606 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    607 
    608 	if (WSMUXCTL(minr)) {
    609 		/* control device */
    610 		return (1);
    611 	}
    612 
    613 	if (sc->sc_base.me_evp == NULL) {
    614 #ifdef DIAGNOSTIC
    615 		printf("wsmuxkqfilter: not open\n");
    616 #endif
    617 		return (1);
    618 	}
    619 
    620 	return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
    621 }
    622 
    623 /*
    624  * Add mux unit as a child to muxsc.
    625  */
    626 int
    627 wsmux_add_mux(int unit, struct wsmux_softc *muxsc)
    628 {
    629 	struct wsmux_softc *sc, *m;
    630 
    631 	sc = wsmux_getmux(unit);
    632 	if (sc == NULL)
    633 		return (ENXIO);
    634 
    635 	DPRINTF(("wsmux_add_mux: %s(%p) to %s(%p)\n",
    636 		 device_xname(sc->sc_base.me_dv), sc,
    637 		 device_xname(muxsc->sc_base.me_dv), muxsc));
    638 
    639 	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
    640 		return (EBUSY);
    641 
    642 	/* The mux we are adding must not be an ancestor of itself. */
    643 	for (m = muxsc; m != NULL ; m = m->sc_base.me_parent)
    644 		if (m == sc)
    645 			return (EINVAL);
    646 
    647 	return (wsmux_attach_sc(muxsc, &sc->sc_base));
    648 }
    649 
    650 /* Create a new mux softc. */
    651 struct wsmux_softc *
    652 wsmux_create(const char *name, int unit)
    653 {
    654 	struct wsmux_softc *sc;
    655 
    656 	/* XXX This is wrong -- should use autoconfiguraiton framework */
    657 
    658 	DPRINTF(("wsmux_create: allocating\n"));
    659 	sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT|M_ZERO);
    660 	if (sc == NULL)
    661 		return (NULL);
    662 	sc->sc_base.me_dv = malloc(sizeof(struct device), M_DEVBUF, M_NOWAIT|M_ZERO);
    663 	if (sc->sc_base.me_dv == NULL) {
    664 		free(sc, M_DEVBUF);
    665 		return NULL;
    666 	}
    667 	TAILQ_INIT(&sc->sc_cld);
    668 	snprintf(sc->sc_base.me_dv->dv_xname, sizeof sc->sc_base.me_dv->dv_xname,
    669 		 "%s%d", name, unit);
    670 	sc->sc_base.me_dv->dv_private = sc;
    671 	sc->sc_base.me_dv->dv_unit = unit;
    672 	sc->sc_base.me_ops = &wsmux_srcops;
    673 	sc->sc_kbd_layout = KB_NONE;
    674 	return (sc);
    675 }
    676 
    677 /* Attach me as a child to sc. */
    678 int
    679 wsmux_attach_sc(struct wsmux_softc *sc, struct wsevsrc *me)
    680 {
    681 	int error;
    682 
    683 	if (sc == NULL)
    684 		return (EINVAL);
    685 
    686 	DPRINTF(("wsmux_attach_sc: %s(%p): type=%d\n",
    687 		 device_xname(sc->sc_base.me_dv), sc, me->me_ops->type));
    688 
    689 #ifdef DIAGNOSTIC
    690 	if (me->me_parent != NULL) {
    691 		printf("wsmux_attach_sc: busy\n");
    692 		return (EBUSY);
    693 	}
    694 #endif
    695 	me->me_parent = sc;
    696 	TAILQ_INSERT_TAIL(&sc->sc_cld, me, me_next);
    697 
    698 	error = 0;
    699 #if NWSDISPLAY > 0
    700 	if (sc->sc_base.me_dispdv != NULL) {
    701 		/* This is a display mux, so attach the new device to it. */
    702 		DPRINTF(("wsmux_attach_sc: %s: set display %p\n",
    703 			 device_xname(sc->sc_base.me_dv),
    704 			 sc->sc_base.me_dispdv));
    705 		if (me->me_ops->dsetdisplay != NULL) {
    706 			error = wsevsrc_set_display(me, &sc->sc_base);
    707 			/* Ignore that the console already has a display. */
    708 			if (error == EBUSY)
    709 				error = 0;
    710 			if (!error) {
    711 #ifdef WSDISPLAY_COMPAT_RAWKBD
    712 				DPRINTF(("wsmux_attach_sc: %s set rawkbd=%d\n",
    713 					 device_xname(me->me_dv),
    714 					 sc->sc_rawkbd));
    715 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
    716 						    &sc->sc_rawkbd, 0, 0);
    717 #endif
    718 				if (sc->sc_kbd_layout != KB_NONE)
    719 					(void)wsevsrc_ioctl(me,
    720 					    WSKBDIO_SETENCODING,
    721 					    &sc->sc_kbd_layout, FWRITE, 0);
    722 			}
    723 		}
    724 	}
    725 #endif
    726 	if (sc->sc_base.me_evp != NULL) {
    727 		/* Mux is open, so open the new subdevice */
    728 		DPRINTF(("wsmux_attach_sc: %s: calling open of %s\n",
    729 			 device_xname(sc->sc_base.me_dv),
    730 			 device_xname(me->me_dv)));
    731 		error = wsevsrc_open(me, sc->sc_base.me_evp);
    732 	} else {
    733 		DPRINTF(("wsmux_attach_sc: %s not open\n",
    734 			 device_xname(sc->sc_base.me_dv)));
    735 	}
    736 
    737 	if (error) {
    738 		me->me_parent = NULL;
    739 		TAILQ_REMOVE(&sc->sc_cld, me, me_next);
    740 	}
    741 
    742 	DPRINTF(("wsmux_attach_sc: %s(%p) done, error=%d\n",
    743 		 device_xname(sc->sc_base.me_dv), sc, error));
    744 	return (error);
    745 }
    746 
    747 /* Remove me from the parent. */
    748 void
    749 wsmux_detach_sc(struct wsevsrc *me)
    750 {
    751 	struct wsmux_softc *sc = me->me_parent;
    752 
    753 	DPRINTF(("wsmux_detach_sc: %s(%p) parent=%p\n",
    754 		 device_xname(me->me_dv), me, sc));
    755 
    756 #ifdef DIAGNOSTIC
    757 	if (sc == NULL) {
    758 		printf("wsmux_detach_sc: %s has no parent\n",
    759 		       device_xname(me->me_dv));
    760 		return;
    761 	}
    762 #endif
    763 
    764 #if NWSDISPLAY > 0
    765 	if (sc->sc_base.me_dispdv != NULL) {
    766 		if (me->me_ops->dsetdisplay != NULL)
    767 			/* ignore error, there's nothing we can do */
    768 			(void)wsevsrc_set_display(me, NULL);
    769 	} else
    770 #endif
    771 		if (me->me_evp != NULL) {
    772 		DPRINTF(("wsmux_detach_sc: close\n"));
    773 		/* mux device is open, so close multiplexee */
    774 		(void)wsevsrc_close(me);
    775 	}
    776 
    777 	TAILQ_REMOVE(&sc->sc_cld, me, me_next);
    778 	me->me_parent = NULL;
    779 
    780 	DPRINTF(("wsmux_detach_sc: done sc=%p\n", sc));
    781 }
    782 
    783 /*
    784  * Display ioctl() of a mux via the parent mux.
    785  */
    786 int
    787 wsmux_do_displayioctl(device_t dv, u_long cmd, void *data, int flag,
    788 		      struct lwp *l)
    789 {
    790 	struct wsmux_softc *sc = device_private(dv);
    791 	struct wsevsrc *me;
    792 	int error, ok;
    793 
    794 	DPRINTF(("wsmux_displayioctl: %s: sc=%p, cmd=%08lx\n",
    795 		 device_xname(sc->sc_base.me_dv), sc, cmd));
    796 
    797 #ifdef WSDISPLAY_COMPAT_RAWKBD
    798 	if (cmd == WSKBDIO_SETMODE) {
    799 		sc->sc_rawkbd = *(int *)data;
    800 		DPRINTF(("wsmux_displayioctl: rawkbd = %d\n", sc->sc_rawkbd));
    801 	}
    802 #endif
    803 
    804 	/*
    805 	 * Return 0 if any of the ioctl() succeeds, otherwise the last error.
    806 	 * Return EPASSTHROUGH if no mux component accepts the ioctl.
    807 	 */
    808 	error = EPASSTHROUGH;
    809 	ok = 0;
    810 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    811 		DPRINTF(("wsmux_displayioctl: me=%p\n", me));
    812 #ifdef DIAGNOSTIC
    813 		if (me->me_parent != sc) {
    814 			printf("wsmux_displayioctl: bad child %p\n", me);
    815 			continue;
    816 		}
    817 #endif
    818 		if (me->me_ops->ddispioctl != NULL) {
    819 			error = wsevsrc_display_ioctl(me, cmd, data, flag, l);
    820 			DPRINTF(("wsmux_displayioctl: me=%p dev=%s ==> %d\n",
    821 				 me, device_xname(me->me_dv), error));
    822 			if (!error)
    823 				ok = 1;
    824 		}
    825 	}
    826 	if (ok)
    827 		error = 0;
    828 
    829 	return (error);
    830 }
    831 
    832 #if NWSDISPLAY > 0
    833 /*
    834  * Set display of a mux via the parent mux.
    835  */
    836 int
    837 wsmux_evsrc_set_display(device_t dv, struct wsevsrc *ame)
    838 {
    839 	struct wsmux_softc *muxsc = (struct wsmux_softc *)ame;
    840 	struct wsmux_softc *sc = device_private(dv);
    841 	device_t displaydv = muxsc ? muxsc->sc_base.me_dispdv : NULL;
    842 
    843 	DPRINTF(("wsmux_set_display: %s: displaydv=%p\n",
    844 		 device_xname(sc->sc_base.me_dv), displaydv));
    845 
    846 	if (displaydv != NULL) {
    847 		if (sc->sc_base.me_dispdv != NULL)
    848 			return (EBUSY);
    849 	} else {
    850 		if (sc->sc_base.me_dispdv == NULL)
    851 			return (ENXIO);
    852 	}
    853 
    854 	return wsmux_set_display(sc, displaydv);
    855 }
    856 
    857 int
    858 wsmux_set_display(struct wsmux_softc *sc, device_t displaydv)
    859 {
    860 	device_t odisplaydv;
    861 	struct wsevsrc *me;
    862 	struct wsmux_softc *nsc = displaydv ? sc : NULL;
    863 	int error, ok;
    864 
    865 	odisplaydv = sc->sc_base.me_dispdv;
    866 	sc->sc_base.me_dispdv = displaydv;
    867 
    868 	if (displaydv)
    869 		aprint_verbose_dev(sc->sc_base.me_dv, "connecting to %s\n",
    870 		       device_xname(displaydv));
    871 	ok = 0;
    872 	error = 0;
    873 	TAILQ_FOREACH(me, &sc->sc_cld,me_next) {
    874 #ifdef DIAGNOSTIC
    875 		if (me->me_parent != sc) {
    876 			printf("wsmux_set_display: bad child parent %p\n", me);
    877 			continue;
    878 		}
    879 #endif
    880 		if (me->me_ops->dsetdisplay != NULL) {
    881 			error = wsevsrc_set_display(me, &nsc->sc_base);
    882 			DPRINTF(("wsmux_set_display: m=%p dev=%s error=%d\n",
    883 				 me, device_xname(me->me_dv), error));
    884 			if (!error) {
    885 				ok = 1;
    886 #ifdef WSDISPLAY_COMPAT_RAWKBD
    887 				DPRINTF(("wsmux_set_display: %s set rawkbd=%d\n",
    888 					 device_xname(me->me_dv), sc->sc_rawkbd));
    889 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
    890 						    &sc->sc_rawkbd, 0, 0);
    891 #endif
    892 			}
    893 		}
    894 	}
    895 	if (ok)
    896 		error = 0;
    897 
    898 	if (displaydv == NULL)
    899 		aprint_verbose("%s: disconnecting from %s\n",
    900 		       device_xname(sc->sc_base.me_dv),
    901 		       device_xname(odisplaydv));
    902 
    903 	return (error);
    904 }
    905 #endif /* NWSDISPLAY > 0 */
    906