Home | History | Annotate | Line # | Download | only in wscons
wsmux.c revision 1.67
      1 /*	$NetBSD: wsmux.c,v 1.67 2025/03/23 12:23:49 hans 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.67 2025/03/23 12:23:49 hans 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 #include <sys/device_impl.h>	/* XXX autoconf abuse */
     67 
     68 #include "opt_wsdisplay_compat.h"
     69 
     70 #include <dev/wscons/wsconsio.h>
     71 #include <dev/wscons/wsksymdef.h>
     72 #include <dev/wscons/wseventvar.h>
     73 #include <dev/wscons/wscons_callbacks.h>
     74 #include <dev/wscons/wsmuxvar.h>
     75 
     76 #include "ioconf.h"
     77 
     78 #ifdef WSMUX_DEBUG
     79 #define DPRINTF(x)	if (wsmuxdebug) printf x
     80 #define DPRINTFN(n,x)	if (wsmuxdebug > (n)) printf x
     81 int	wsmuxdebug = 0;
     82 #else
     83 #define DPRINTF(x)
     84 #define DPRINTFN(n,x)
     85 #endif
     86 
     87 /*
     88  * The wsmux pseudo device is used to multiplex events from several wsmouse,
     89  * wskbd, and/or wsmux devices together.
     90  * The devices connected together form a tree with muxes in the interior
     91  * and real devices (mouse and kbd) at the leaves.  The special case of
     92  * a tree with one node (mux or other) is supported as well.
     93  * Only the device at the root of the tree can be opened (if a non-root
     94  * device is opened the subtree rooted at that point is severed from the
     95  * containing tree).  When the root is opened it allocates a wseventvar
     96  * struct which all the nodes in the tree will send their events too.
     97  * An ioctl() performed on the root is propagated to all the nodes.
     98  * There are also ioctl() operations to add and remove nodes from a tree.
     99  */
    100 
    101 static int wsmux_mux_open(struct wsevsrc *, struct wseventvar *);
    102 static int wsmux_mux_close(struct wsevsrc *);
    103 
    104 static void wsmux_do_open(struct wsmux_softc *, struct wseventvar *);
    105 
    106 static void wsmux_do_close(struct wsmux_softc *);
    107 #if NWSDISPLAY > 0
    108 static int wsmux_evsrc_set_display(device_t, struct wsevsrc *);
    109 #else
    110 #define wsmux_evsrc_set_display NULL
    111 #endif
    112 
    113 static int wsmux_do_displayioctl(device_t dev, u_long cmd,
    114 				 void *data, int flag, struct lwp *l);
    115 static int wsmux_do_ioctl(device_t, u_long, void *,int,struct lwp *);
    116 
    117 static int wsmux_add_mux(int, struct wsmux_softc *);
    118 
    119 #define WSMUXDEV(n) ((n) & 0x7f)
    120 #define WSMUXCTL(n) ((n) & 0x80)
    121 
    122 dev_type_open(wsmuxopen);
    123 dev_type_close(wsmuxclose);
    124 dev_type_read(wsmuxread);
    125 dev_type_ioctl(wsmuxioctl);
    126 dev_type_poll(wsmuxpoll);
    127 dev_type_kqfilter(wsmuxkqfilter);
    128 
    129 const struct cdevsw wsmux_cdevsw = {
    130 	.d_open = wsmuxopen,
    131 	.d_close = wsmuxclose,
    132 	.d_read = wsmuxread,
    133 	.d_write = nowrite,
    134 	.d_ioctl = wsmuxioctl,
    135 	.d_stop = nostop,
    136 	.d_tty = notty,
    137 	.d_poll = wsmuxpoll,
    138 	.d_mmap = nommap,
    139 	.d_kqfilter = wsmuxkqfilter,
    140 	.d_discard = nodiscard,
    141 	.d_flag = D_OTHER
    142 };
    143 
    144 struct wssrcops wsmux_srcops = {
    145 	WSMUX_MUX,
    146 	wsmux_mux_open, wsmux_mux_close, wsmux_do_ioctl, wsmux_do_displayioctl,
    147 	wsmux_evsrc_set_display
    148 };
    149 
    150 /* From upper level */
    151 void
    152 wsmuxattach(int n)
    153 {
    154 }
    155 
    156 /* Keep track of all muxes that have been allocated */
    157 static struct wsmux_softc **wsmuxdevs = NULL;
    158 static int nwsmux = 0;
    159 
    160 /* Return mux n, create if necessary */
    161 struct wsmux_softc *
    162 wsmux_getmux(int n)
    163 {
    164 	struct wsmux_softc *sc;
    165 
    166 	n = WSMUXDEV(n);	/* limit range */
    167 
    168 	/* Make sure there is room for mux n in the table */
    169 	if (n >= nwsmux) {
    170 		void *new;
    171 
    172 		new = realloc(wsmuxdevs, (n + 1) * sizeof(*wsmuxdevs),
    173 		    M_DEVBUF, M_ZERO | M_WAITOK);
    174 		wsmuxdevs = new;
    175 		nwsmux = n + 1;
    176 	}
    177 
    178 	sc = wsmuxdevs[n];
    179 	if (sc == NULL) {
    180 		sc = wsmux_create("wsmux", n);
    181 		wsmuxdevs[n] = sc;
    182 	}
    183 	return (sc);
    184 }
    185 
    186 /*
    187  * open() of the pseudo device from device table.
    188  */
    189 int
    190 wsmuxopen(dev_t dev, int flags, int mode, struct lwp *l)
    191 {
    192 	struct wsmux_softc *sc;
    193 	struct wseventvar *evar;
    194 	int minr, unit;
    195 
    196 	minr = minor(dev);
    197 	unit = WSMUXDEV(minr);
    198 	sc = wsmux_getmux(unit);
    199 	if (sc == NULL)
    200 		return (ENXIO);
    201 
    202 	DPRINTF(("wsmuxopen: %s: sc=%p l=%p\n",
    203 		 device_xname(sc->sc_base.me_dv), sc, l));
    204 
    205 	if (WSMUXCTL(minr)) {
    206 		/* This is the control device which does not allow reads. */
    207 		if (flags & FREAD)
    208 			return (EINVAL);
    209 		return (0);
    210 	}
    211 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    212 		/* Allow write only open */
    213 		return (0);
    214 
    215 	if (sc->sc_base.me_parent != NULL) {
    216 		/* Grab the mux out of the greedy hands of the parent mux. */
    217 		DPRINTF(("wsmuxopen: detach\n"));
    218 		wsmux_detach_sc(&sc->sc_base);
    219 	}
    220 
    221 	if (sc->sc_base.me_evp != NULL)
    222 		/* Already open. */
    223 		return (EBUSY);
    224 
    225 	evar = &sc->sc_base.me_evar;
    226 	wsevent_init(evar, l->l_proc);
    227 #ifdef WSDISPLAY_COMPAT_RAWKBD
    228 	sc->sc_rawkbd = 0;
    229 #endif
    230 
    231 	wsmux_do_open(sc, evar);
    232 
    233 	return (0);
    234 }
    235 
    236 /*
    237  * Open of a mux via the parent mux.
    238  */
    239 int
    240 wsmux_mux_open(struct wsevsrc *me, struct wseventvar *evar)
    241 {
    242 	struct wsmux_softc *sc = (struct wsmux_softc *)me;
    243 
    244 	if (sc->sc_base.me_evp != NULL) {
    245 #ifdef DIAGNOSTIC
    246 		printf("wsmux_mux_open: busy\n");
    247 #endif
    248 		return (EBUSY);
    249 	}
    250 	if (sc->sc_base.me_parent == NULL) {
    251 #ifdef DIAGNOSTIC
    252 		printf("wsmux_mux_open: no parent\n");
    253 #endif
    254 		return (EINVAL);
    255 	}
    256 
    257 	wsmux_do_open(sc, evar);
    258 
    259 	return (0);
    260 }
    261 
    262 /* Common part of opening a mux. */
    263 void
    264 wsmux_do_open(struct wsmux_softc *sc, struct wseventvar *evar)
    265 {
    266 	struct wsevsrc *me;
    267 
    268 	sc->sc_base.me_evp = evar; /* remember event variable, mark as open */
    269 
    270 	/* Open all children. */
    271 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    272 		DPRINTF(("wsmuxopen: %s: m=%p dev=%s\n",
    273 			 device_xname(sc->sc_base.me_dv), me,
    274 			 device_xname(me->me_dv)));
    275 		if (me->me_evp != NULL) {
    276 #ifdef DIAGNOSTIC
    277 			printf("wsmuxopen: dev already in use\n");
    278 #endif
    279 			continue;
    280 		}
    281 		if (me->me_parent != sc) {
    282 #ifdef DIAGNOSTIC
    283 			printf("wsmux_do_open: bad child=%p\n", me);
    284 #endif
    285 			continue;
    286 		}
    287 #ifdef DIAGNOSTIC
    288 		int error = wsevsrc_open(me, evar);
    289 
    290 		if (error) {
    291 			DPRINTF(("wsmuxopen: open failed %d\n", error));
    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 		if (me->me_parent != sc) {
    350 #ifdef DIAGNOSTIC
    351 			printf("wsmuxclose: bad child=%p\n", me);
    352 #endif
    353 			continue;
    354 		}
    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 		case WSMUX_BELL:
    460 			return (wsbell_add_mux(d->idx, sc));
    461 		default:
    462 			return (EINVAL);
    463 		}
    464 	case WSMUXIO_REMOVE_DEVICE:
    465 		DPRINTF(("%s: rem type=%d, no=%d\n",
    466 			 device_xname(sc->sc_base.me_dv), d->type, d->idx));
    467 		/* Locate the device */
    468 		TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    469 			if (me->me_ops->type == d->type &&
    470 			    device_unit(me->me_dv) == d->idx) {
    471 				DPRINTF(("wsmux_do_ioctl: detach\n"));
    472 				wsmux_detach_sc(me);
    473 				return (0);
    474 			}
    475 		}
    476 		return (EINVAL);
    477 #undef d
    478 
    479 	case WSMUXIO_LIST_DEVICES:
    480 		DPRINTF(("%s: list\n", device_xname(sc->sc_base.me_dv)));
    481 		l = (struct wsmux_device_list *)data;
    482 		n = 0;
    483 		TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    484 			if (n >= WSMUX_MAXDEV)
    485 				break;
    486 			l->devices[n].type = me->me_ops->type;
    487 			l->devices[n].idx = device_unit(me->me_dv);
    488 			n++;
    489 		}
    490 		l->ndevices = n;
    491 		return (0);
    492 #ifdef WSDISPLAY_COMPAT_RAWKBD
    493 	case WSKBDIO_SETMODE:
    494 		sc->sc_rawkbd = *(int *)data;
    495 		DPRINTF(("wsmux_do_ioctl: save rawkbd = %d\n", sc->sc_rawkbd));
    496 		break;
    497 #endif
    498 
    499 	case WSKBDIO_SETVERSION:
    500 	case WSMOUSEIO_SETVERSION:
    501 	case WSDISPLAYIO_SETVERSION:
    502 		DPRINTF(("%s: WSxxxIO_SETVERSION\n",
    503 			device_xname(sc->sc_base.me_dv)));
    504 		evar = sc->sc_base.me_evp;
    505 		if (evar == NULL)
    506 			return (EINVAL);
    507 		return wsevent_setversion(evar, *(int *)data);
    508 
    509 	case FIONBIO:
    510 		DPRINTF(("%s: FIONBIO\n", device_xname(sc->sc_base.me_dv)));
    511 		return (0);
    512 
    513 	case FIOASYNC:
    514 		DPRINTF(("%s: FIOASYNC\n", device_xname(sc->sc_base.me_dv)));
    515 		evar = sc->sc_base.me_evp;
    516 		if (evar == NULL)
    517 			return (EINVAL);
    518 		evar->async = *(int *)data != 0;
    519 		return (0);
    520 	case FIOSETOWN:
    521 		DPRINTF(("%s: FIOSETOWN\n", device_xname(sc->sc_base.me_dv)));
    522 		evar = sc->sc_base.me_evp;
    523 		if (evar == NULL)
    524 			return (EINVAL);
    525 		if (-*(int *)data != evar->io->p_pgid
    526 		    && *(int *)data != evar->io->p_pid)
    527 			return (EPERM);
    528 		return (0);
    529 	case TIOCSPGRP:
    530 		DPRINTF(("%s: TIOCSPGRP\n", device_xname(sc->sc_base.me_dv)));
    531 		evar = sc->sc_base.me_evp;
    532 		if (evar == NULL)
    533 			return (EINVAL);
    534 		if (*(int *)data != evar->io->p_pgid)
    535 			return (EPERM);
    536 		return (0);
    537 	default:
    538 		DPRINTF(("%s: unknown\n", device_xname(sc->sc_base.me_dv)));
    539 		break;
    540 	}
    541 
    542 	if (sc->sc_base.me_evp == NULL
    543 #if NWSDISPLAY > 0
    544 	    && sc->sc_base.me_dispdv == NULL
    545 #endif
    546 	    )
    547 		return (EACCES);
    548 
    549 	/* Return 0 if any of the ioctl() succeeds, otherwise the last error */
    550 	error = 0;
    551 	ok = 0;
    552 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    553 		/* XXX check evp? */
    554 		if (me->me_parent != sc) {
    555 #ifdef DIAGNOSTIC
    556 			printf("wsmux_do_ioctl: bad child %p\n", me);
    557 #endif
    558 			continue;
    559 		}
    560 		error = wsevsrc_ioctl(me, cmd, data, flag, lwp);
    561 		DPRINTF(("wsmux_do_ioctl: %s: me=%p dev=%s ==> %d\n",
    562 			 device_xname(sc->sc_base.me_dv), me,
    563 			 device_xname(me->me_dv), error));
    564 		if (!error)
    565 			ok = 1;
    566 	}
    567 	if (ok) {
    568 		error = 0;
    569 		if (cmd == WSKBDIO_SETENCODING) {
    570 			sc->sc_kbd_layout = *((kbd_t *)data);
    571 		}
    572 
    573 	}
    574 
    575 	return (error);
    576 }
    577 
    578 /*
    579  * poll() of the pseudo device from device table.
    580  */
    581 int
    582 wsmuxpoll(dev_t dev, int events, struct lwp *l)
    583 {
    584 	int minr = minor(dev);
    585 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    586 
    587 	if (WSMUXCTL(minr)) {
    588 		/* control device */
    589 		return (0);
    590 	}
    591 
    592 	if (sc->sc_base.me_evp == NULL) {
    593 #ifdef DIAGNOSTIC
    594 		printf("wsmuxpoll: not open\n");
    595 #endif
    596 		return (POLLHUP);
    597 	}
    598 
    599 	return (wsevent_poll(sc->sc_base.me_evp, events, l));
    600 }
    601 
    602 /*
    603  * kqfilter() of the pseudo device from device table.
    604  */
    605 int
    606 wsmuxkqfilter(dev_t dev, struct knote *kn)
    607 {
    608 	int minr = minor(dev);
    609 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    610 
    611 	if (WSMUXCTL(minr)) {
    612 		/* control device */
    613 		return (1);
    614 	}
    615 
    616 	if (sc->sc_base.me_evp == NULL) {
    617 #ifdef DIAGNOSTIC
    618 		printf("wsmuxkqfilter: not open\n");
    619 #endif
    620 		return (1);
    621 	}
    622 
    623 	return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
    624 }
    625 
    626 /*
    627  * Add mux unit as a child to muxsc.
    628  */
    629 int
    630 wsmux_add_mux(int unit, struct wsmux_softc *muxsc)
    631 {
    632 	struct wsmux_softc *sc, *m;
    633 
    634 	sc = wsmux_getmux(unit);
    635 	if (sc == NULL)
    636 		return (ENXIO);
    637 
    638 	DPRINTF(("wsmux_add_mux: %s(%p) to %s(%p)\n",
    639 		 device_xname(sc->sc_base.me_dv), sc,
    640 		 device_xname(muxsc->sc_base.me_dv), muxsc));
    641 
    642 	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
    643 		return (EBUSY);
    644 
    645 	/* The mux we are adding must not be an ancestor of itself. */
    646 	for (m = muxsc; m != NULL ; m = m->sc_base.me_parent)
    647 		if (m == sc)
    648 			return (EINVAL);
    649 
    650 	return (wsmux_attach_sc(muxsc, &sc->sc_base));
    651 }
    652 
    653 /* Create a new mux softc. */
    654 struct wsmux_softc *
    655 wsmux_create(const char *name, int unit)
    656 {
    657 	struct wsmux_softc *sc;
    658 
    659 	/* XXX This is wrong -- should use autoconfiguration framework */
    660 
    661 	DPRINTF(("wsmux_create: allocating\n"));
    662 	sc = malloc(sizeof *sc, M_DEVBUF, M_WAITOK|M_ZERO);
    663 	sc->sc_base.me_dv = malloc(sizeof(struct device), M_DEVBUF,
    664 	    M_WAITOK|M_ZERO);
    665 	TAILQ_INIT(&sc->sc_cld);
    666 	snprintf(sc->sc_base.me_dv->dv_xname,
    667 	    sizeof sc->sc_base.me_dv->dv_xname, "%s%d", name, unit);
    668 	sc->sc_base.me_dv->dv_private = sc;
    669 	sc->sc_base.me_dv->dv_unit = unit;
    670 	sc->sc_base.me_ops = &wsmux_srcops;
    671 	sc->sc_kbd_layout = KB_NONE;
    672 	return (sc);
    673 }
    674 
    675 /* Attach me as a child to sc. */
    676 int
    677 wsmux_attach_sc(struct wsmux_softc *sc, struct wsevsrc *me)
    678 {
    679 	int error;
    680 
    681 	if (sc == NULL)
    682 		return (EINVAL);
    683 
    684 	DPRINTF(("wsmux_attach_sc: %s(%p): type=%d\n",
    685 		 device_xname(sc->sc_base.me_dv), sc, me->me_ops->type));
    686 
    687 	if (me->me_parent != NULL) {
    688 #ifdef DIAGNOSTIC
    689 		printf("wsmux_attach_sc: busy\n");
    690 #endif
    691 		return (EBUSY);
    692 	}
    693 	me->me_parent = sc;
    694 	TAILQ_INSERT_TAIL(&sc->sc_cld, me, me_next);
    695 
    696 	error = 0;
    697 #if NWSDISPLAY > 0
    698 	if (sc->sc_base.me_dispdv != NULL) {
    699 		/* This is a display mux, so attach the new device to it. */
    700 		DPRINTF(("wsmux_attach_sc: %s: set display %p\n",
    701 			 device_xname(sc->sc_base.me_dv),
    702 			 sc->sc_base.me_dispdv));
    703 		if (me->me_ops->dsetdisplay != NULL) {
    704 			error = wsevsrc_set_display(me, &sc->sc_base);
    705 			/* Ignore that the console already has a display. */
    706 			if (error == EBUSY)
    707 				error = 0;
    708 			if (!error) {
    709 #ifdef WSDISPLAY_COMPAT_RAWKBD
    710 				DPRINTF(("wsmux_attach_sc: %s set rawkbd=%d\n",
    711 					 device_xname(me->me_dv),
    712 					 sc->sc_rawkbd));
    713 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
    714 						    &sc->sc_rawkbd, 0, 0);
    715 #endif
    716 				if (sc->sc_kbd_layout != KB_NONE)
    717 					(void)wsevsrc_ioctl(me,
    718 					    WSKBDIO_SETENCODING,
    719 					    &sc->sc_kbd_layout, FWRITE, 0);
    720 			}
    721 		}
    722 	}
    723 #endif
    724 	if (sc->sc_base.me_evp != NULL) {
    725 		/* Mux is open, so open the new subdevice */
    726 		DPRINTF(("wsmux_attach_sc: %s: calling open of %s\n",
    727 			 device_xname(sc->sc_base.me_dv),
    728 			 device_xname(me->me_dv)));
    729 		error = wsevsrc_open(me, sc->sc_base.me_evp);
    730 	} else {
    731 		DPRINTF(("wsmux_attach_sc: %s not open\n",
    732 			 device_xname(sc->sc_base.me_dv)));
    733 	}
    734 
    735 	if (error) {
    736 		me->me_parent = NULL;
    737 		TAILQ_REMOVE(&sc->sc_cld, me, me_next);
    738 	}
    739 
    740 	DPRINTF(("wsmux_attach_sc: %s(%p) done, error=%d\n",
    741 		 device_xname(sc->sc_base.me_dv), sc, error));
    742 	return (error);
    743 }
    744 
    745 /* Remove me from the parent. */
    746 void
    747 wsmux_detach_sc(struct wsevsrc *me)
    748 {
    749 	struct wsmux_softc *sc = me->me_parent;
    750 
    751 	DPRINTF(("wsmux_detach_sc: %s(%p) parent=%p\n",
    752 		 device_xname(me->me_dv), me, sc));
    753 
    754 	if (sc == NULL) {
    755 #ifdef DIAGNOSTIC
    756 		printf("wsmux_detach_sc: %s has no parent\n",
    757 		       device_xname(me->me_dv));
    758 #endif
    759 		return;
    760 	}
    761 
    762 #if NWSDISPLAY > 0
    763 	if (sc->sc_base.me_dispdv != NULL) {
    764 		if (me->me_ops->dsetdisplay != NULL)
    765 			/* ignore error, there's nothing we can do */
    766 			(void)wsevsrc_set_display(me, NULL);
    767 	} else
    768 #endif
    769 		if (me->me_evp != NULL) {
    770 		DPRINTF(("wsmux_detach_sc: close\n"));
    771 		/* mux device is open, so close multiplexee */
    772 		(void)wsevsrc_close(me);
    773 	}
    774 
    775 	TAILQ_REMOVE(&sc->sc_cld, me, me_next);
    776 	me->me_parent = NULL;
    777 
    778 	DPRINTF(("wsmux_detach_sc: done sc=%p\n", sc));
    779 }
    780 
    781 /*
    782  * Display ioctl() of a mux via the parent mux.
    783  */
    784 int
    785 wsmux_do_displayioctl(device_t dv, u_long cmd, void *data, int flag,
    786 		      struct lwp *l)
    787 {
    788 	struct wsmux_softc *sc = device_private(dv);
    789 	struct wsevsrc *me;
    790 	int error, ok;
    791 
    792 	DPRINTF(("wsmux_displayioctl: %s: sc=%p, cmd=%08lx\n",
    793 		 device_xname(sc->sc_base.me_dv), sc, cmd));
    794 
    795 #ifdef WSDISPLAY_COMPAT_RAWKBD
    796 	if (cmd == WSKBDIO_SETMODE) {
    797 		sc->sc_rawkbd = *(int *)data;
    798 		DPRINTF(("wsmux_displayioctl: rawkbd = %d\n", sc->sc_rawkbd));
    799 	}
    800 #endif
    801 
    802 	/*
    803 	 * Return 0 if any of the ioctl() succeeds, otherwise the last error.
    804 	 * Return EPASSTHROUGH if no mux component accepts the ioctl.
    805 	 */
    806 	error = EPASSTHROUGH;
    807 	ok = 0;
    808 	TAILQ_FOREACH(me, &sc->sc_cld, me_next) {
    809 		DPRINTF(("wsmux_displayioctl: me=%p\n", me));
    810 		if (me->me_parent != sc) {
    811 #ifdef DIAGNOSTIC
    812 			printf("wsmux_displayioctl: bad child %p\n", me);
    813 #endif
    814 			continue;
    815 		}
    816 		if (me->me_ops->ddispioctl != NULL) {
    817 			error = wsevsrc_display_ioctl(me, cmd, data, flag, l);
    818 			DPRINTF(("wsmux_displayioctl: me=%p dev=%s ==> %d\n",
    819 				 me, device_xname(me->me_dv), error));
    820 			if (!error)
    821 				ok = 1;
    822 		}
    823 	}
    824 	if (ok)
    825 		error = 0;
    826 
    827 	return (error);
    828 }
    829 
    830 #if NWSDISPLAY > 0
    831 /*
    832  * Set display of a mux via the parent mux.
    833  */
    834 int
    835 wsmux_evsrc_set_display(device_t dv, struct wsevsrc *ame)
    836 {
    837 	struct wsmux_softc *muxsc = (struct wsmux_softc *)ame;
    838 	struct wsmux_softc *sc = device_private(dv);
    839 	device_t displaydv = muxsc ? muxsc->sc_base.me_dispdv : NULL;
    840 
    841 	DPRINTF(("wsmux_set_display: %s: displaydv=%p\n",
    842 		 device_xname(sc->sc_base.me_dv), displaydv));
    843 
    844 	if (displaydv != NULL) {
    845 		if (sc->sc_base.me_dispdv != NULL)
    846 			return (EBUSY);
    847 	} else {
    848 		if (sc->sc_base.me_dispdv == NULL)
    849 			return (ENXIO);
    850 	}
    851 
    852 	return wsmux_set_display(sc, displaydv);
    853 }
    854 
    855 int
    856 wsmux_set_display(struct wsmux_softc *sc, device_t displaydv)
    857 {
    858 	device_t odisplaydv;
    859 	struct wsevsrc *me;
    860 	struct wsmux_softc *nsc = displaydv ? sc : NULL;
    861 	int error, ok;
    862 
    863 	odisplaydv = sc->sc_base.me_dispdv;
    864 	sc->sc_base.me_dispdv = displaydv;
    865 
    866 	if (displaydv)
    867 		aprint_verbose_dev(sc->sc_base.me_dv, "connecting to %s\n",
    868 		       device_xname(displaydv));
    869 	ok = 0;
    870 	error = 0;
    871 	TAILQ_FOREACH(me, &sc->sc_cld,me_next) {
    872 		if (me->me_parent != sc) {
    873 #ifdef DIAGNOSTIC
    874 			printf("wsmux_set_display: bad child parent %p\n", me);
    875 #endif
    876 			continue;
    877 		}
    878 		if (me->me_ops->dsetdisplay != NULL) {
    879 			error = wsevsrc_set_display(me, &nsc->sc_base);
    880 			DPRINTF(("wsmux_set_display: m=%p dev=%s error=%d\n",
    881 				 me, device_xname(me->me_dv), error));
    882 			if (!error) {
    883 				ok = 1;
    884 #ifdef WSDISPLAY_COMPAT_RAWKBD
    885 				DPRINTF(("wsmux_set_display: %s set rawkbd=%d\n",
    886 					 device_xname(me->me_dv), sc->sc_rawkbd));
    887 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
    888 						    &sc->sc_rawkbd, 0, 0);
    889 #endif
    890 			}
    891 		}
    892 	}
    893 	if (ok)
    894 		error = 0;
    895 
    896 	if (displaydv == NULL)
    897 		aprint_verbose("%s: disconnecting from %s\n",
    898 		       device_xname(sc->sc_base.me_dv),
    899 		       device_xname(odisplaydv));
    900 
    901 	return (error);
    902 }
    903 #endif /* NWSDISPLAY > 0 */
    904