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