Home | History | Annotate | Line # | Download | only in wscons
wsmux.c revision 1.19
      1 /*	$NetBSD: wsmux.c,v 1.19 2001/10/27 00:34:57 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Lennart Augustsson <augustss (at) carlstedt.se>
      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 "wsdisplay.h"
     47 #include "wsmux.h"
     48 #include "wskbd.h"
     49 #include "wsmouse.h"
     50 
     51 #include <sys/param.h>
     52 #include <sys/conf.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/fcntl.h>
     55 #include <sys/kernel.h>
     56 #include <sys/malloc.h>
     57 #include <sys/proc.h>
     58 #include <sys/queue.h>
     59 #include <sys/syslog.h>
     60 #include <sys/systm.h>
     61 #include <sys/tty.h>
     62 #include <sys/signalvar.h>
     63 #include <sys/device.h>
     64 
     65 #include "opt_wsdisplay_compat.h"
     66 
     67 #include <dev/wscons/wsconsio.h>
     68 #include <dev/wscons/wseventvar.h>
     69 #include <dev/wscons/wscons_callbacks.h>
     70 #include <dev/wscons/wsmuxvar.h>
     71 
     72 #ifdef WSMUX_DEBUG
     73 #define DPRINTF(x)	if (wsmuxdebug) printf x
     74 #define DPRINTFN(n,x)	if (wsmuxdebug > (n)) printf x
     75 int	wsmuxdebug = 0;
     76 #else
     77 #define DPRINTF(x)
     78 #define DPRINTFN(n,x)
     79 #endif
     80 
     81 /*
     82  * The wsmux pseudo device is used to multiplex events from several wsmouse,
     83  * wskbd, and/or wsmux devices together.
     84  * The devices connected together form a tree with muxes in the interior
     85  * and real devices (mouse and kbd) at the leaves.  The special case of
     86  * a tree with one node (mux or other) is supported as well.
     87  * Only the device at the root of the tree can be opened (if a non-root
     88  * device is opened the subtree rooted at that point is severed from the
     89  * containing tree).  When the root is opened it allocates a wseventvar
     90  * struct which all the nodes in the tree will send their events too.
     91  * An ioctl() performed on the root is propagated to all the nodes.
     92  * There are also ioctl() operations to add and remove nodes from a tree.
     93  */
     94 
     95 static int wsmux_mux_open(struct wsevsrc *, struct wseventvar *);
     96 static int wsmux_mux_close(struct wsevsrc *);
     97 
     98 static void wsmux_do_open(struct wsmux_softc *, struct wseventvar *);
     99 
    100 static void wsmux_do_close(struct wsmux_softc *);
    101 #if NWSDISPLAY > 0
    102 static int wsmux_set_display(struct device *, struct wsevsrc *);
    103 #else
    104 #define wsmux_set_display NULL
    105 #endif
    106 
    107 static int wsmux_do_displayioctl(struct device *dev, u_long cmd,
    108 				 caddr_t data, int flag, struct proc *p);
    109 static int wsmux_do_ioctl(struct device *, u_long, caddr_t,int,struct proc *);
    110 
    111 static int wsmux_add_mux(int, struct wsmux_softc *);
    112 
    113 void wsmuxattach(int);
    114 
    115 #define WSMUXDEV(n) ((n) & 0x7f)
    116 #define WSMUXCTL(n) ((n) & 0x80)
    117 
    118 cdev_decl(wsmux);
    119 
    120 struct wssrcops wsmux_srcops = {
    121 	WSMUX_MUX,
    122 	wsmux_mux_open, wsmux_mux_close, wsmux_do_ioctl, wsmux_do_displayioctl,
    123 	wsmux_set_display
    124 };
    125 
    126 /* From upper level */
    127 void
    128 wsmuxattach(int n)
    129 {
    130 }
    131 
    132 /* Keep track of all muxes that have been allocated */
    133 static int nwsmux = 0;
    134 static struct wsmux_softc **wsmuxdevs;
    135 
    136 /* Return mux n, create if necessary */
    137 struct wsmux_softc *
    138 wsmux_getmux(int n)
    139 {
    140 	struct wsmux_softc *sc;
    141 	int i;
    142 	void *new;
    143 
    144 	n = WSMUXDEV(n);	/* limit range */
    145 
    146 	/* Make sure there is room for mux n in the table */
    147 	if (n >= nwsmux) {
    148 		i = nwsmux;
    149 		nwsmux = n + 1;
    150 		if (i != 0)
    151 			new = realloc(wsmuxdevs, nwsmux * sizeof (*wsmuxdevs),
    152 				      M_DEVBUF, M_NOWAIT);
    153 		else
    154 			new = malloc(nwsmux * sizeof (*wsmuxdevs),
    155 				     M_DEVBUF, M_NOWAIT);
    156 		if (new == NULL) {
    157 			printf("wsmux_getmux: no memory for mux %d\n", n);
    158 			return (NULL);
    159 		}
    160 		wsmuxdevs = new;
    161 		for (; i < nwsmux; i++)
    162 			wsmuxdevs[i] = NULL;
    163 	}
    164 
    165 	sc = wsmuxdevs[n];
    166 	if (sc == NULL) {
    167 		sc = wsmux_create("wsmux", n);
    168 		if (sc == NULL)
    169 			printf("wsmux: attach out of memory\n");
    170 		wsmuxdevs[n] = sc;
    171 	}
    172 	return (sc);
    173 }
    174 
    175 /*
    176  * open() of the pseudo device from device table.
    177  */
    178 int
    179 wsmuxopen(dev_t dev, int flags, int mode, struct proc *p)
    180 {
    181 	struct wsmux_softc *sc;
    182 	struct wseventvar *evar;
    183 	int minr, unit;
    184 
    185 	minr = minor(dev);
    186 	unit = WSMUXDEV(minr);
    187 	sc = wsmux_getmux(unit);
    188 	if (sc == NULL)
    189 		return (ENXIO);
    190 
    191 	DPRINTF(("wsmuxopen: %s: sc=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
    192 		 sc, p));
    193 
    194 	if (WSMUXCTL(minr)) {
    195 		/* This is the control device which does not allow reads. */
    196 		if (flags & FREAD)
    197 			return (EINVAL);
    198 		return (0);
    199 	}
    200 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    201 		/* Allow write only open */
    202 		return (0);
    203 
    204 	if (sc->sc_base.me_parent != NULL) {
    205 		/* Grab the mux out of the greedy hands of the parent mux. */
    206 		wsmux_detach_sc(&sc->sc_base);
    207 	}
    208 
    209 	if (sc->sc_base.me_evp != NULL)
    210 		/* Already open. */
    211 		return (EBUSY);
    212 
    213 	evar = &sc->sc_base.me_evar;
    214 	wsevent_init(evar);
    215 	evar->io = p;
    216 #ifdef WSDISPLAY_COMPAT_RAWKBD
    217 	sc->sc_rawkbd = 0;
    218 #endif
    219 
    220 	wsmux_do_open(sc, evar);
    221 
    222 	return (0);
    223 }
    224 
    225 /*
    226  * Open of a mux via the parent mux.
    227  */
    228 int
    229 wsmux_mux_open(struct wsevsrc *me, struct wseventvar *evar)
    230 {
    231 	struct wsmux_softc *sc = (struct wsmux_softc *)me;
    232 
    233 #ifdef DIAGNOSTIC
    234 	if (sc->sc_base.me_evp != NULL) {
    235 		printf("wsmux_mux_open: busy\n");
    236 		return (EBUSY);
    237 	}
    238 	if (sc->sc_base.me_parent == NULL) {
    239 		printf("wsmux_mux_open: no parent\n");
    240 		return (EINVAL);
    241 	}
    242 #endif
    243 
    244 	wsmux_do_open(sc, evar);
    245 
    246 	return (0);
    247 }
    248 
    249 /* Common part of opening a mux. */
    250 void
    251 wsmux_do_open(struct wsmux_softc *sc, struct wseventvar *evar)
    252 {
    253 	struct wsevsrc *me;
    254 
    255 	sc->sc_base.me_evp = evar; /* remember event variable, mark as open */
    256 
    257 	/* Open all children. */
    258 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    259 		DPRINTF(("wsmuxopen: %s: m=%p dev=%s\n",
    260 			 sc->sc_base.me_dv.dv_xname, me, me->me_dv.dv_xname));
    261 #ifdef DIAGNOSTIC
    262 		if (me->me_evp != NULL) {
    263 			printf("wsmuxopen: dev already in use\n");
    264 			continue;
    265 		}
    266 		if (me->me_parent != sc) {
    267 			printf("wsmux_do_open: bad child=%p\n", me);
    268 			continue;
    269 		}
    270 		{
    271 		int error = me->me_ops->dopen(me, evar);
    272 		if (error) {
    273 			DPRINTF(("wsmuxopen: open failed %d\n", error));
    274 		}
    275 		}
    276 #else
    277 		/* ignore errors, failing children will not be marked open */
    278 		(void)me->me_ops->dopen(me, evar);
    279 #endif
    280 	}
    281 }
    282 
    283 /*
    284  * close() of the pseudo device from device table.
    285  */
    286 int
    287 wsmuxclose(dev_t dev, int flags, int mode, struct proc *p)
    288 {
    289 	int minr = minor(dev);
    290 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    291 	struct wseventvar *evar = sc->sc_base.me_evp;
    292 
    293 	if (WSMUXCTL(minr))
    294 		/* control device */
    295 		return (0);
    296 	if (evar == NULL)
    297 		/* Not open for read */
    298 		return (0);
    299 
    300 	sc->sc_base.me_evp = NULL;
    301 	wsevent_fini(evar);
    302 	wsmux_do_close(sc);
    303 	return (0);
    304 }
    305 
    306 /*
    307  * Close of a mux via the parent mux.
    308  */
    309 int
    310 wsmux_mux_close(struct wsevsrc *me)
    311 {
    312 	me->me_evp = NULL;
    313 	wsmux_do_close((struct wsmux_softc *)me);
    314 	return (0);
    315 }
    316 
    317 /* Common part of closing a mux. */
    318 void
    319 wsmux_do_close(struct wsmux_softc *sc)
    320 {
    321 	struct wsevsrc *me;
    322 
    323 	DPRINTF(("wsmuxclose: %s: sc=%p\n", sc->sc_base.me_dv.dv_xname, sc));
    324 
    325 	/* Close all the children. */
    326 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    327 		DPRINTF(("wsmuxclose %s: m=%p dev=%s\n",
    328 			 sc->sc_base.me_dv.dv_xname, me, me->me_dv.dv_xname));
    329 #ifdef DIAGNOSTIC
    330 		if (me->me_parent != sc) {
    331 			printf("wsmuxclose: bad child=%p\n", me);
    332 			continue;
    333 		}
    334 #endif
    335 		(void)me->me_ops->dclose(me);
    336 		me->me_evp = NULL;
    337 	}
    338 }
    339 
    340 /*
    341  * read() of the pseudo device from device table.
    342  */
    343 int
    344 wsmuxread(dev_t dev, struct uio *uio, int flags)
    345 {
    346 	int minr = minor(dev);
    347 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    348 	struct wseventvar *evar;
    349 	int error;
    350 
    351 	if (WSMUXCTL(minr)) {
    352 		/* control device */
    353 		return (EINVAL);
    354 	}
    355 
    356 	evar = sc->sc_base.me_evp;
    357 	if (evar == NULL) {
    358 #ifdef DIAGNOSTIC
    359 		/* XXX can we get here? */
    360 		printf("wsmuxread: not open\n");
    361 #endif
    362 		return (EINVAL);
    363 	}
    364 
    365 	DPRINTFN(5,("wsmuxread: %s event read evar=%p\n",
    366 		    sc->sc_base.me_dv.dv_xname, evar));
    367 	error = wsevent_read(evar, uio, flags);
    368 	DPRINTFN(5,("wsmuxread: %s event read ==> error=%d\n",
    369 		    sc->sc_base.me_dv.dv_xname, error));
    370 	return (error);
    371 }
    372 
    373 /*
    374  * ioctl of the pseudo device from device table.
    375  */
    376 int
    377 wsmuxioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    378 {
    379 	int u = WSMUXDEV(minor(dev));
    380 
    381 	return wsmux_do_ioctl(&wsmuxdevs[u]->sc_base.me_dv, cmd, data, flag, p);
    382 }
    383 
    384 /*
    385  * ioctl of a mux via the parent mux, continuation of wsmuxioctl().
    386  */
    387 int
    388 wsmux_do_ioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
    389 	       struct proc *p)
    390 {
    391 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    392 	struct wsevsrc *me;
    393 	int error, ok;
    394 	int s, put, get, n;
    395 	struct wseventvar *evar;
    396 	struct wscons_event *ev;
    397 	struct timeval thistime;
    398 	struct wsmux_device_list *l;
    399 
    400 	DPRINTF(("wsmux_do_ioctl: %s: enter sc=%p, cmd=%08lx\n",
    401 		 sc->sc_base.me_dv.dv_xname, sc, cmd));
    402 
    403 	switch (cmd) {
    404 	case WSMUXIO_INJECTEVENT:
    405 		/* Inject an event, e.g., from moused. */
    406 		DPRINTF(("%s: inject\n", sc->sc_base.me_dv.dv_xname));
    407 
    408 		evar = sc->sc_base.me_evp;
    409 		if (evar == NULL) /* XXX is this an error? */
    410 			return (EACCES);
    411 
    412 		s = spltty();
    413 		get = evar->get;
    414 		put = evar->put;
    415 		if (++put % WSEVENT_QSIZE == get) {
    416 			put--;
    417 			splx(s);
    418 			return (ENOSPC);
    419 		}
    420 		if (put >= WSEVENT_QSIZE)
    421 			put = 0;
    422 		ev = &evar->q[put];
    423 		*ev = *(struct wscons_event *)data;
    424 		microtime(&thistime);
    425 		TIMEVAL_TO_TIMESPEC(&thistime, &ev->time);
    426 		evar->put = put;
    427 		WSEVENT_WAKEUP(evar);
    428 		splx(s);
    429 		return (0);
    430 	case WSMUXIO_ADD_DEVICE:
    431 #define d ((struct wsmux_device *)data)
    432 		DPRINTF(("%s: add type=%d, no=%d\n", sc->sc_base.me_dv.dv_xname,
    433 			 d->type, d->idx));
    434 		switch (d->type) {
    435 #if NWSMOUSE > 0
    436 		case WSMUX_MOUSE:
    437 			return (wsmouse_add_mux(d->idx, sc));
    438 #endif
    439 #if NWSKBD > 0
    440 		case WSMUX_KBD:
    441 			return (wskbd_add_mux(d->idx, sc));
    442 #endif
    443 		case WSMUX_MUX:
    444 			return (wsmux_add_mux(d->idx, sc));
    445 		default:
    446 			return (EINVAL);
    447 		}
    448 	case WSMUXIO_REMOVE_DEVICE:
    449 		DPRINTF(("%s: rem type=%d, no=%d\n", sc->sc_base.me_dv.dv_xname,
    450 			 d->type, d->idx));
    451 		/* Locate the device */
    452 		CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    453 			if (me->me_ops->type == d->type &&
    454 			    me->me_dv.dv_unit == d->idx) {
    455 				wsmux_detach_sc(me);
    456 				return (0);
    457 			}
    458 		}
    459 		return (EINVAL);
    460 #undef d
    461 
    462 	case WSMUXIO_LIST_DEVICES:
    463 		DPRINTF(("%s: list\n", sc->sc_base.me_dv.dv_xname));
    464 		l = (struct wsmux_device_list *)data;
    465 		n = 0;
    466 		CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    467 			if (n >= WSMUX_MAXDEV)
    468 				break;
    469 			l->devices[n].type = me->me_ops->type;
    470 			l->devices[n].idx = me->me_dv.dv_unit;
    471 			n++;
    472 		}
    473 		l->ndevices = n;
    474 		return (0);
    475 #ifdef WSDISPLAY_COMPAT_RAWKBD
    476 	case WSKBDIO_SETMODE:
    477 		sc->sc_rawkbd = *(int *)data;
    478 		DPRINTF(("wsmux_do_ioctl: save rawkbd = %d\n", sc->sc_rawkbd));
    479 		break;
    480 #endif
    481 	case FIONBIO:
    482 		DPRINTF(("%s: FIONBIO\n", sc->sc_base.me_dv.dv_xname));
    483 		return (0);
    484 
    485 	case FIOASYNC:
    486 		DPRINTF(("%s: FIOASYNC\n", sc->sc_base.me_dv.dv_xname));
    487 		evar = sc->sc_base.me_evp;
    488 		if (evar == NULL)
    489 			return (EINVAL);
    490 		evar->async = *(int *)data != 0;
    491 		return (0);
    492 	case TIOCSPGRP:
    493 		DPRINTF(("%s: TIOCSPGRP\n", sc->sc_base.me_dv.dv_xname));
    494 		evar = sc->sc_base.me_evp;
    495 		if (evar == NULL)
    496 			return (EINVAL);
    497 		if (*(int *)data != evar->io->p_pgid)
    498 			return (EPERM);
    499 		return (0);
    500 	default:
    501 		DPRINTF(("%s: unknown\n", sc->sc_base.me_dv.dv_xname));
    502 		break;
    503 	}
    504 
    505 	if (sc->sc_base.me_evp == NULL
    506 #if NWSDISPLAY > 0
    507 	    && sc->sc_base.me_dispdv == NULL
    508 #endif
    509 	    )
    510 		return (EACCES);
    511 
    512 	/* Return 0 if any of the ioctl() succeeds, otherwise the last error */
    513 	error = 0;
    514 	ok = 0;
    515 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    516 #ifdef DIAGNOSTIC
    517 		/* XXX check evp? */
    518 		if (me->me_parent != sc) {
    519 			printf("wsmux_do_ioctl: bad child %p\n", me);
    520 			continue;
    521 		}
    522 #endif
    523 		error = wsevsrc_ioctl(me, cmd, data, flag, p);
    524 		DPRINTF(("wsmux_do_ioctl: %s: me=%p dev=%s ==> %d\n",
    525 			 sc->sc_base.me_dv.dv_xname, me, me->me_dv.dv_xname,
    526 			 error));
    527 		if (!error)
    528 			ok = 1;
    529 	}
    530 	if (ok)
    531 		error = 0;
    532 
    533 	return (error);
    534 }
    535 
    536 /*
    537  * poll() of the pseudo device from device table.
    538  */
    539 int
    540 wsmuxpoll(dev_t dev, int events, struct proc *p)
    541 {
    542 	int minr = minor(dev);
    543 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    544 
    545 	if (WSMUXCTL(minr)) {
    546 		/* control device */
    547 		return (EINVAL);
    548 	}
    549 
    550 	if (sc->sc_base.me_evp == NULL) {
    551 #ifdef DIAGNOSTIC
    552 		printf("wsmuxpoll: not open\n");
    553 #endif
    554 		return (EACCES);
    555 	}
    556 
    557 	return (wsevent_poll(sc->sc_base.me_evp, events, p));
    558 }
    559 
    560 /*
    561  * Add mux unit as a child to muxsc.
    562  */
    563 int
    564 wsmux_add_mux(int unit, struct wsmux_softc *muxsc)
    565 {
    566 	struct wsmux_softc *sc, *m;
    567 
    568 	sc = wsmux_getmux(unit);
    569 	if (sc == NULL)
    570 		return (ENXIO);
    571 
    572 	DPRINTF(("wsmux_add_mux: %s(%p) to %s(%p)\n",
    573 		 sc->sc_base.me_dv.dv_xname, sc, muxsc->sc_base.me_dv.dv_xname,
    574 		 muxsc));
    575 
    576 	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
    577 		return (EBUSY);
    578 
    579 	/* The mux we are adding must not be an ancestor of itself. */
    580 	for (m = muxsc; m != NULL ; m = m->sc_base.me_parent)
    581 		if (m == sc)
    582 			return (EINVAL);
    583 
    584 	return (wsmux_attach_sc(muxsc, &sc->sc_base));
    585 }
    586 
    587 /* Create a new mux softc. */
    588 struct wsmux_softc *
    589 wsmux_create(const char *name, int unit)
    590 {
    591 	struct wsmux_softc *sc;
    592 
    593 	DPRINTF(("wsmux_create: allocating\n"));
    594 	sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT);
    595 	if (sc == NULL)
    596 		return (NULL);
    597 	memset(sc, 0, sizeof *sc);
    598 	CIRCLEQ_INIT(&sc->sc_cld);
    599 	snprintf(sc->sc_base.me_dv.dv_xname, sizeof sc->sc_base.me_dv.dv_xname,
    600 		 "%s%d", name, unit);
    601 	sc->sc_base.me_dv.dv_unit = unit;
    602 	sc->sc_base.me_ops = &wsmux_srcops;
    603 	return (sc);
    604 }
    605 
    606 /* Attach me as a child to sc. */
    607 int
    608 wsmux_attach_sc(struct wsmux_softc *sc, struct wsevsrc *me)
    609 {
    610 	int error;
    611 
    612 	if (sc == NULL)
    613 		return (EINVAL);
    614 
    615 	DPRINTF(("wsmux_attach_sc: %s: type=%d\n",
    616 		 sc->sc_base.me_dv.dv_xname, me->me_ops->type));
    617 
    618 #ifdef DIAGNOSTIC
    619 	if (me->me_parent != NULL) {
    620 		printf("wsmux_attach_sc: busy\n");
    621 		return (EBUSY);
    622 	}
    623 #endif
    624 	me->me_parent = sc;
    625 	CIRCLEQ_INSERT_TAIL(&sc->sc_cld, me, me_next);
    626 
    627 	error = 0;
    628 #if NWSDISPLAY > 0
    629 	if (sc->sc_base.me_dispdv != NULL) {
    630 		/* This is a display mux, so attach the new device to it. */
    631 		DPRINTF(("wsmux_attach_sc: %s: set display %p\n",
    632 			 sc->sc_base.me_dv.dv_xname, sc->sc_base.me_dispdv));
    633 		if (me->me_ops->dsetdisplay != NULL) {
    634 			error = wsevsrc_set_display(me, &sc->sc_base);
    635 			/* Ignore that the console already has a display. */
    636 			if (error == EBUSY)
    637 				error = 0;
    638 			if (!error) {
    639 #ifdef WSDISPLAY_COMPAT_RAWKBD
    640 				DPRINTF(("wsmux_attach_sc: %s set rawkbd=%d\n",
    641 					 me->me_dv.dv_xname, sc->sc_rawkbd));
    642 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
    643 						    &sc->sc_rawkbd, 0, 0);
    644 #endif
    645 			}
    646 		}
    647 	}
    648 #endif
    649 	if (sc->sc_base.me_evp != NULL) {
    650 		/* Mux is open, so open the new subdevice */
    651 		DPRINTF(("wsmux_attach_sc: %s: calling open of %s\n",
    652 			 sc->sc_base.me_dv.dv_xname, me->me_dv.dv_xname));
    653 		error = me->me_ops->dopen(me, sc->sc_base.me_evp);
    654 	} else {
    655 		DPRINTF(("wsmux_attach_sc: %s not open\n",
    656 			 sc->sc_base.me_dv.dv_xname));
    657 	}
    658 
    659 	if (error) {
    660 		me->me_parent = NULL;
    661 		CIRCLEQ_REMOVE(&sc->sc_cld, me, me_next);
    662 	}
    663 
    664 	DPRINTF(("wsmux_attach_sc: done sc=%p, error=%d\n", sc, error));
    665 	return (error);
    666 }
    667 
    668 /* Remove me from the parent. */
    669 void
    670 wsmux_detach_sc(struct wsevsrc *me)
    671 {
    672 	struct wsmux_softc *sc = me->me_parent;
    673 
    674 	DPRINTF(("wsmux_detach_sc: %s\n", sc->sc_base.me_dv.dv_xname));
    675 
    676 #ifdef DIAGNOSTIC
    677 	if (sc == NULL) {
    678 		printf("wsmux_detach_sc: not allocated\n");
    679 		return;
    680 	}
    681 #endif
    682 
    683 #if NWSDISPLAY > 0
    684 	if (sc->sc_base.me_dispdv != NULL) {
    685 		if (me->me_ops->dsetdisplay != NULL)
    686 			/* ignore error, there's nothing we can do */
    687 			(void)wsevsrc_set_display(me, NULL);
    688 	} else
    689 #endif
    690 		if (me->me_evp != NULL) {
    691 		DPRINTF(("wsmux_detach_sc: close\n"));
    692 		/* mux device is open, so close multiplexee */
    693 		(void)me->me_ops->dclose(me);
    694 	}
    695 
    696 	CIRCLEQ_REMOVE(&sc->sc_cld, me, me_next);
    697 	me->me_parent = NULL;
    698 
    699 	DPRINTF(("wsmux_detach_sc: done sc=%p\n", sc));
    700 }
    701 
    702 /*
    703  * Display ioctl() of a mux via the parent mux.
    704  */
    705 int
    706 wsmux_do_displayioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
    707 		      struct proc *p)
    708 {
    709 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    710 	struct wsevsrc *me;
    711 	int error, ok;
    712 
    713 	DPRINTF(("wsmux_displayioctl: %s: sc=%p, cmd=%08lx\n",
    714 		 sc->sc_base.me_dv.dv_xname, sc, cmd));
    715 
    716 #ifdef WSDISPLAY_COMPAT_RAWKBD
    717 	if (cmd == WSKBDIO_SETMODE) {
    718 		sc->sc_rawkbd = *(int *)data;
    719 		DPRINTF(("wsmux_displayioctl: rawkbd = %d\n", sc->sc_rawkbd));
    720 	}
    721 #endif
    722 
    723 	/*
    724 	 * Return 0 if any of the ioctl() succeeds, otherwise the last error.
    725 	 * Return -1 if no mux component accepts the ioctl.
    726 	 */
    727 	error = -1;
    728 	ok = 0;
    729 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    730 		DPRINTF(("wsmux_displayioctl: me=%p\n", me));
    731 #ifdef DIAGNOSTIC
    732 		if (me->me_parent != sc) {
    733 			printf("wsmux_displayioctl: bad child %p\n", me);
    734 			continue;
    735 		}
    736 #endif
    737 		if (me->me_ops->ddispioctl != NULL) {
    738 			error = wsevsrc_display_ioctl(me, cmd, data, flag, p);
    739 			DPRINTF(("wsmux_displayioctl: me=%p dev=%s ==> %d\n",
    740 				 me, me->me_dv.dv_xname, error));
    741 			if (!error)
    742 				ok = 1;
    743 		}
    744 	}
    745 	if (ok)
    746 		error = 0;
    747 
    748 	return (error);
    749 }
    750 
    751 #if NWSDISPLAY > 0
    752 /*
    753  * Set display of a mux via the parent mux.
    754  */
    755 int
    756 wsmux_set_display(struct device *dv, struct wsevsrc *ame)
    757 {
    758 	struct wsmux_softc *muxsc = (struct wsmux_softc *)ame;
    759 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    760 	struct wsmux_softc *nsc = muxsc ? sc : NULL;
    761 	struct device *displaydv = muxsc ? muxsc->sc_base.me_dispdv : NULL;
    762 	struct device *odisplaydv;
    763 	struct wsevsrc *me;
    764 	int error, ok;
    765 
    766 	DPRINTF(("wsmux_set_display: %s: displaydv=%p\n",
    767 		 sc->sc_base.me_dv.dv_xname, displaydv));
    768 
    769 	if (displaydv != NULL) {
    770 		if (sc->sc_base.me_dispdv != NULL)
    771 			return (EBUSY);
    772 	} else {
    773 		if (sc->sc_base.me_dispdv == NULL)
    774 			return (ENXIO);
    775 	}
    776 
    777 	odisplaydv = sc->sc_base.me_dispdv;
    778 	sc->sc_base.me_dispdv = displaydv;
    779 
    780 	if (displaydv)
    781 		printf("%s: connecting to %s\n",
    782 		       sc->sc_base.me_dv.dv_xname, displaydv->dv_xname);
    783 	ok = 0;
    784 	error = 0;
    785 	CIRCLEQ_FOREACH(me, &sc->sc_cld,me_next) {
    786 #ifdef DIAGNOSTIC
    787 		if (me->me_parent != sc) {
    788 			printf("wsmux_set_display: bad child parent %p\n", me);
    789 			continue;
    790 		}
    791 #endif
    792 		if (me->me_ops->dsetdisplay != NULL) {
    793 			error = wsevsrc_set_display(me, &nsc->sc_base);
    794 			DPRINTF(("wsmux_set_display: m=%p dev=%s error=%d\n",
    795 				 me, me->me_dv.dv_xname, error));
    796 			if (!error) {
    797 				ok = 1;
    798 #ifdef WSDISPLAY_COMPAT_RAWKBD
    799 				DPRINTF(("wsmux_set_display: on %s set rawkbd=%d\n",
    800 					 me->me_dv.dv_xname, sc->sc_rawkbd));
    801 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
    802 						    &sc->sc_rawkbd, 0, 0);
    803 #endif
    804 			}
    805 		}
    806 	}
    807 	if (ok)
    808 		error = 0;
    809 
    810 	if (displaydv == NULL)
    811 		printf("%s: disconnecting from %s\n",
    812 		       sc->sc_base.me_dv.dv_xname, odisplaydv->dv_xname);
    813 
    814 	return (error);
    815 }
    816 #endif /* NWSDISPLAY > 0 */
    817