Home | History | Annotate | Line # | Download | only in wscons
wsmux.c revision 1.14
      1 /*
      2   TODO:
      3   use method to get sc of muxee, common code for add&rem
      4  */
      5 
      6 /*	$NetBSD: wsmux.c,v 1.14 2001/10/24 14:07:33 augustss Exp $	*/
      7 
      8 /*
      9  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     10  * All rights reserved.
     11  *
     12  * Author: Lennart Augustsson <augustss (at) carlstedt.se>
     13  *         Carlstedt Research & Technology
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  * 3. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *        This product includes software developed by the NetBSD
     26  *        Foundation, Inc. and its contributors.
     27  * 4. Neither the name of The NetBSD Foundation nor the names of its
     28  *    contributors may be used to endorse or promote products derived
     29  *    from this software without specific prior written permission.
     30  *
     31  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     32  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     33  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     34  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     35  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     36  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     37  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     39  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGE.
     42  */
     43 
     44 /*
     45  * wscons mux device.
     46  *
     47  * The mux device is a collection of real mice and keyboards and acts as
     48  * a merge point for all the events from the different real devices.
     49  */
     50 
     51 #include "wsdisplay.h"
     52 #include "wsmux.h"
     53 #include "wskbd.h"
     54 #include "wsmouse.h"
     55 
     56 #include <sys/param.h>
     57 #include <sys/conf.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/fcntl.h>
     60 #include <sys/kernel.h>
     61 #include <sys/malloc.h>
     62 #include <sys/proc.h>
     63 #include <sys/queue.h>
     64 #include <sys/syslog.h>
     65 #include <sys/systm.h>
     66 #include <sys/tty.h>
     67 #include <sys/signalvar.h>
     68 #include <sys/device.h>
     69 
     70 #include "opt_wsdisplay_compat.h"
     71 
     72 #include <dev/wscons/wsconsio.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 int	wsmuxdebug = 0;
     80 #else
     81 #define DPRINTF(x)
     82 #endif
     83 
     84 /*
     85  * The wsmux pseudo device is used to multiplex events from several wsmouse,
     86  * wskbd, and/or wsmux devices together.
     87  * The devices connected together form a tree with muxes in the interior
     88  * and real devices (mouse and kbd) at the leaves.  The special case of
     89  * a tree with one node (mux or other) is supported as well.
     90  * Only the device at the root of the tree can be opened (if a non-root
     91  * device is opened the subtree rooted at that point is severed from the
     92  * containing tree).  When the root is opened it allocates a wseventvar
     93  * struct which all the nodes in the tree will send their events too.
     94  * An ioctl() performed on the root is propagated to all the nodes.
     95  * There are also ioctl() operations to add and remove nodes from a tree.
     96  */
     97 
     98 static int wsmux_mux_open(struct wsevsrc *, struct wseventvar *);
     99 static int wsmux_mux_close(struct wsevsrc *);
    100 
    101 static void wsmux_do_open(struct wsmux_softc *, struct wseventvar *);
    102 
    103 static void wsmux_do_close(struct wsmux_softc *);
    104 #if NWSDISPLAY > 0
    105 static int wsmux_set_display(struct device *, struct wsevsrc *);
    106 #else
    107 #define wsmux_set_display NULL
    108 #endif
    109 
    110 static int wsmux_do_displayioctl(struct device *dev, u_long cmd,
    111 				 caddr_t data, int flag, struct proc *p);
    112 static int wsmux_do_ioctl(struct device *, u_long, caddr_t,int,struct proc *);
    113 
    114 static int wsmux_add_mux(int, struct wsmux_softc *);
    115 
    116 void wsmuxattach(int);
    117 
    118 #define WSMUXDEV(n) ((n) & 0x7f)
    119 #define WSMUXCTL(n) ((n) & 0x80)
    120 
    121 cdev_decl(wsmux);
    122 
    123 struct wssrcops wsmux_srcops = {
    124 	WSMUX_MUX,
    125 	wsmux_mux_open, wsmux_mux_close, wsmux_do_ioctl, wsmux_do_displayioctl,
    126 	wsmux_set_display
    127 };
    128 
    129 /* From upper level */
    130 void
    131 wsmuxattach(int n)
    132 {
    133 }
    134 
    135 /* Keep track of all muxes that have been allocated */
    136 static int nwsmux = 0;
    137 static struct wsmux_softc **wsmuxdevs;
    138 
    139 /* Return mux n, create if necessary */
    140 struct wsmux_softc *
    141 wsmux_getmux(int n)
    142 {
    143 	struct wsmux_softc *sc;
    144 	int i;
    145 	void *new;
    146 
    147 	n = WSMUXDEV(n);	/* limit range */
    148 
    149 	/* Make sure there is room for mux n in the table */
    150 	if (n >= nwsmux) {
    151 		i = nwsmux;
    152 		nwsmux = n + 1;
    153 		if (i != 0)
    154 			new = realloc(wsmuxdevs, nwsmux * sizeof (*wsmuxdevs),
    155 				      M_DEVBUF, M_NOWAIT);
    156 		else
    157 			new = malloc(nwsmux * sizeof (*wsmuxdevs),
    158 				     M_DEVBUF, M_NOWAIT);
    159 		if (new == NULL) {
    160 			printf("wsmux_getmux: no memory for mux %d\n", n);
    161 			return (NULL);
    162 		}
    163 		wsmuxdevs = new;
    164 		for (; i < nwsmux; i++)
    165 			wsmuxdevs[i] = NULL;
    166 	}
    167 
    168 	sc = wsmuxdevs[n];
    169 	if (sc == NULL) {
    170 		sc = wsmux_create("wsmux", n);
    171 		if (sc == NULL)
    172 			printf("wsmux: attach out of memory\n");
    173 		wsmuxdevs[n] = sc;
    174 	}
    175 	return (sc);
    176 }
    177 
    178 /*
    179  * open() of the pseudo device from device table.
    180  */
    181 int
    182 wsmuxopen(dev_t dev, int flags, int mode, struct proc *p)
    183 {
    184 	struct wsmux_softc *sc;
    185 	struct wseventvar *evar;
    186 	int minr, unit;
    187 
    188 	minr = minor(dev);
    189 	unit = WSMUXDEV(minr);
    190 	sc = wsmux_getmux(unit);
    191 	if (sc == NULL)
    192 		return (ENXIO);
    193 
    194 	DPRINTF(("wsmuxopen: %s: sc=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
    195 		 sc, p));
    196 
    197 	if (WSMUXCTL(minr)) {
    198 		/* This is the control device which does not allow reads. */
    199 		if (flags & FREAD)
    200 			return (EINVAL);
    201 		return (0);
    202 	}
    203 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    204 		/* Allow write only open */
    205 		return (0);
    206 
    207 	if (sc->sc_base.me_parent != NULL) {
    208 		/* Grab the mux out of the greedy hands of the parent mux. */
    209 		wsmux_detach_sc(&sc->sc_base);
    210 	}
    211 
    212 	if (sc->sc_base.me_evp != NULL)
    213 		/* Already open. */
    214 		return (EBUSY);
    215 
    216 	evar = wsevent_alloc();
    217 	evar->io = p;
    218 	sc->sc_rawkbd = 0;
    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_free(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 
    349 	if (WSMUXCTL(minr)) {
    350 		/* control device */
    351 		return (EINVAL);
    352 	}
    353 
    354 	if (sc->sc_base.me_evp == NULL) {
    355 #ifdef DIAGNOSTIC
    356 		/* XXX can we get here? */
    357 		printf("wsmuxread: not open\n");
    358 #endif
    359 		return (EINVAL);
    360 	}
    361 
    362 	return (wsevent_read(sc->sc_base.me_evp, uio, flags));
    363 }
    364 
    365 /*
    366  * ioctl of the pseudo device from device table.
    367  */
    368 int
    369 wsmuxioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    370 {
    371 	int u = WSMUXDEV(minor(dev));
    372 
    373 	return wsmux_do_ioctl(&wsmuxdevs[u]->sc_base.me_dv, cmd, data, flag, p);
    374 }
    375 
    376 /*
    377  * ioctl of a mux via the parent mux, continuation of wsmuxioctl().
    378  */
    379 int
    380 wsmux_do_ioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
    381 	       struct proc *p)
    382 {
    383 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    384 	struct wsevsrc *me;
    385 	int error, ok;
    386 	int s, put, get, n;
    387 	struct wseventvar *evar;
    388 	struct wscons_event *ev;
    389 	struct timeval thistime;
    390 	struct wsmux_device_list *l;
    391 
    392 	DPRINTF(("wsmux_do_ioctl: %s: sc=%p, cmd=%08lx\n",
    393 		 sc->sc_base.me_dv.dv_xname, sc, cmd));
    394 
    395 	switch (cmd) {
    396 	case WSMUXIO_INJECTEVENT:
    397 		/* Inject an event, e.g., from moused. */
    398 		DPRINTF(("%s: inject\n", sc->sc_base.me_dv.dv_xname));
    399 
    400 		evar = sc->sc_base.me_evp;
    401 		if (evar == NULL) /* XXX is this an error? */
    402 			return (EACCES);
    403 
    404 		s = spltty();
    405 		get = evar->get;
    406 		put = evar->put;
    407 		if (++put % WSEVENT_QSIZE == get) {
    408 			put--;
    409 			splx(s);
    410 			return (ENOSPC);
    411 		}
    412 		if (put >= WSEVENT_QSIZE)
    413 			put = 0;
    414 		ev = &evar->q[put];
    415 		*ev = *(struct wscons_event *)data;
    416 		microtime(&thistime);
    417 		TIMEVAL_TO_TIMESPEC(&thistime, &ev->time);
    418 		evar->put = put;
    419 		WSEVENT_WAKEUP(evar);
    420 		splx(s);
    421 		return (0);
    422 	case WSMUXIO_ADD_DEVICE:
    423 #define d ((struct wsmux_device *)data)
    424 		DPRINTF(("%s: add type=%d, no=%d\n", sc->sc_base.me_dv.dv_xname,
    425 			 d->type, d->idx));
    426 		switch (d->type) {
    427 #if NWSMOUSE > 0
    428 		case WSMUX_MOUSE:
    429 			return (wsmouse_add_mux(d->idx, sc));
    430 #endif
    431 #if NWSKBD > 0
    432 		case WSMUX_KBD:
    433 			return (wskbd_add_mux(d->idx, sc));
    434 #endif
    435 		case WSMUX_MUX:
    436 			return (wsmux_add_mux(d->idx, sc));
    437 		default:
    438 			return (EINVAL);
    439 		}
    440 	case WSMUXIO_REMOVE_DEVICE:
    441 		DPRINTF(("%s: rem type=%d, no=%d\n", sc->sc_base.me_dv.dv_xname,
    442 			 d->type, d->idx));
    443 		/* Locate the device */
    444 		CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    445 			if (me->me_ops->type == d->type &&
    446 			    me->me_dv.dv_unit == d->idx) {
    447 				wsmux_detach_sc(me);
    448 				return (0);
    449 			}
    450 		}
    451 		return (EINVAL);
    452 #undef d
    453 
    454 	case WSMUXIO_LIST_DEVICES:
    455 		DPRINTF(("%s: list\n", sc->sc_base.me_dv.dv_xname));
    456 		l = (struct wsmux_device_list *)data;
    457 		n = 0;
    458 		CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    459 			if (n >= WSMUX_MAXDEV)
    460 				break;
    461 			l->devices[n].type = me->me_ops->type;
    462 			l->devices[n].idx = me->me_dv.dv_unit;
    463 			n++;
    464 		}
    465 		l->ndevices = n;
    466 		return (0);
    467 #ifdef WSDISPLAY_COMPAT_RAWKBD
    468 	case WSKBDIO_SETMODE:
    469 		sc->sc_rawkbd = *(int *)data;
    470 		DPRINTF(("wsmux_do_ioctl: save rawkbd = %d\n", sc->sc_rawkbd));
    471 		break;
    472 #endif
    473 	case FIOASYNC:
    474 		DPRINTF(("%s: FIOASYNC\n", sc->sc_base.me_dv.dv_xname));
    475 		evar = sc->sc_base.me_evp;
    476 		if (evar == NULL)
    477 			return (EINVAL);
    478 		evar->async = *(int *)data != 0;
    479 		return (0);
    480 	case TIOCSPGRP:
    481 		DPRINTF(("%s: TIOCSPGRP\n", sc->sc_base.me_dv.dv_xname));
    482 		evar = sc->sc_base.me_evp;
    483 		if (evar == NULL)
    484 			return (EINVAL);
    485 		if (*(int *)data != evar->io->p_pgid)
    486 			return (EPERM);
    487 		return (0);
    488 	default:
    489 		DPRINTF(("%s: unknown\n", sc->sc_base.me_dv.dv_xname));
    490 		break;
    491 	}
    492 
    493 	if (sc->sc_base.me_evp == NULL
    494 #if NWSDISPLAY > 0
    495 	    && sc->sc_base.me_dispdv == NULL
    496 #endif
    497 	    )
    498 		return (EACCES);
    499 
    500 	/* Return 0 if any of the ioctl() succeeds, otherwise the last error */
    501 	error = 0;
    502 	ok = 0;
    503 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    504 		DPRINTF(("wsmux_do_ioctl: me=%p\n", me));
    505 #ifdef DIAGNOSTIC
    506 		/* XXX check evp? */
    507 		if (me->me_parent != sc) {
    508 			printf("wsmux_do_ioctl: bad child %p\n", me);
    509 			continue;
    510 		}
    511 #endif
    512 		DPRINTF(("wsmux_do_ioctl: %s: me=%p dev=%s\n",
    513 			 sc->sc_base.me_dv.dv_xname, me, me->me_dv.dv_xname));
    514 		error = wsevsrc_ioctl(me, cmd, data, flag, p);
    515 		if (!error)
    516 			ok = 1;
    517 	}
    518 	if (ok)
    519 		error = 0;
    520 
    521 	return (error);
    522 }
    523 
    524 /*
    525  * poll() of the pseudo device from device table.
    526  */
    527 int
    528 wsmuxpoll(dev_t dev, int events, struct proc *p)
    529 {
    530 	int minr = minor(dev);
    531 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    532 
    533 	if (WSMUXCTL(minr)) {
    534 		/* control device */
    535 		return (EINVAL);
    536 	}
    537 
    538 	if (sc->sc_base.me_evp == NULL) {
    539 #ifdef DIAGNOSTIC
    540 		printf("wsmuxpoll: not open\n");
    541 #endif
    542 		return (EACCES);
    543 	}
    544 
    545 	return (wsevent_poll(sc->sc_base.me_evp, events, p));
    546 }
    547 
    548 /*
    549  * Add mux unit as a child to muxsc.
    550  */
    551 int
    552 wsmux_add_mux(int unit, struct wsmux_softc *muxsc)
    553 {
    554 	struct wsmux_softc *sc, *m;
    555 
    556 	sc = wsmux_getmux(unit);
    557 	if (sc == NULL)
    558 		return (ENXIO);
    559 
    560 	DPRINTF(("wsmux_add_mux: %s(%p) to %s(%p)\n",
    561 		 sc->sc_base.me_dv.dv_xname, sc, muxsc->sc_base.me_dv.dv_xname,
    562 		 muxsc));
    563 
    564 	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
    565 		return (EBUSY);
    566 
    567 	/* The mux we are adding must not be an ancestor of itself. */
    568 	for (m = muxsc; m != NULL ; m = m->sc_base.me_parent)
    569 		if (m == sc)
    570 			return (EINVAL);
    571 
    572 	return (wsmux_attach_sc(muxsc, &sc->sc_base));
    573 }
    574 
    575 /* Create a new mux softc. */
    576 struct wsmux_softc *
    577 wsmux_create(const char *name, int unit)
    578 {
    579 	struct wsmux_softc *sc;
    580 
    581 	DPRINTF(("wsmux_create: allocating\n"));
    582 	sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT);
    583 	if (sc == NULL)
    584 		return (NULL);
    585 	memset(sc, 0, sizeof *sc);
    586 	CIRCLEQ_INIT(&sc->sc_cld);
    587 	snprintf(sc->sc_base.me_dv.dv_xname, sizeof sc->sc_base.me_dv.dv_xname,
    588 		 "%s%d", name, unit);
    589 	sc->sc_base.me_dv.dv_unit = unit;
    590 	sc->sc_base.me_ops = &wsmux_srcops;
    591 	return (sc);
    592 }
    593 
    594 /* Attach me as a child to sc. */
    595 int
    596 wsmux_attach_sc(struct wsmux_softc *sc, struct wsevsrc *me)
    597 {
    598 	int error;
    599 
    600 	if (sc == NULL)
    601 		return (EINVAL);
    602 
    603 	DPRINTF(("wsmux_attach_sc: %s: type=%d\n",
    604 		 sc->sc_base.me_dv.dv_xname, me->me_ops->type));
    605 
    606 #ifdef DIAGNOSTIC
    607 	if (me->me_parent != NULL) {
    608 		printf("wsmux_attach_sc: busy\n");
    609 		return (EBUSY);
    610 	}
    611 #endif
    612 	me->me_parent = sc;
    613 	CIRCLEQ_INSERT_TAIL(&sc->sc_cld, me, me_next);
    614 
    615 #if NWSDISPLAY > 0
    616 	if (sc->sc_base.me_dispdv != NULL) {
    617 		/* This is a display mux, so attach the new device to it. */
    618 		DPRINTF(("wsmux_attach_sc: %s: set display %p\n",
    619 			 sc->sc_base.me_dv.dv_xname, sc->sc_base.me_dispdv));
    620 		error = 0;
    621 		if (me->me_ops->dsetdisplay != NULL) {
    622 			error = wsevsrc_set_display(me, &sc->sc_base);
    623 			/* Ignore that the console already has a display. */
    624 			if (error == EBUSY)
    625 				error = 0;
    626 			if (!error) {
    627 #ifdef WSDISPLAY_COMPAT_RAWKBD
    628 				DPRINTF(("wsmux_attach_sc: on %s set rawkbd=%d\n",
    629 					 me->me_dv.dv_xname, sc->sc_rawkbd));
    630 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
    631 						    &sc->sc_rawkbd, 0, 0);
    632 #endif
    633 			}
    634 		}
    635 	} else
    636 #endif
    637 		if (sc->sc_base.me_evp != NULL) {
    638 		/* Mux is open, so open the new subdevice */
    639 		DPRINTF(("wsmux_attach_sc: %s: calling open of %s\n",
    640 			 sc->sc_base.me_dv.dv_xname, me->me_dv.dv_xname));
    641 		error = me->me_ops->dopen(me, sc->sc_base.me_evp);
    642 	} else {
    643 		DPRINTF(("wsmux_attach_sc: %s not open\n",
    644 			 sc->sc_base.me_dv.dv_xname));
    645 		error = 0;
    646 	}
    647 
    648 	if (error) {
    649 		me->me_parent = NULL;
    650 		CIRCLEQ_REMOVE(&sc->sc_cld, me, me_next);
    651 	}
    652 
    653 	DPRINTF(("wsmux_attach_sc: done sc=%p, error=%d\n", sc, error));
    654 	return (error);
    655 }
    656 
    657 /* Remove me from the parent. */
    658 void
    659 wsmux_detach_sc(struct wsevsrc *me)
    660 {
    661 	struct wsmux_softc *sc = me->me_parent;
    662 
    663 	DPRINTF(("wsmux_detach_sc: %s\n", sc->sc_base.me_dv.dv_xname));
    664 
    665 #ifdef DIAGNOSTIC
    666 	if (sc == NULL) {
    667 		printf("wsmux_detach_sc: not allocated\n");
    668 		return;
    669 	}
    670 #endif
    671 
    672 #if NWSDISPLAY > 0
    673 	if (sc->sc_base.me_dispdv != NULL) {
    674 		if (me->me_ops->dsetdisplay != NULL)
    675 			/* ignore error, there's nothing we can do */
    676 			(void)wsevsrc_set_display(me, NULL);
    677 	} else
    678 #endif
    679 		if (me->me_evp != NULL) {
    680 		DPRINTF(("wsmux_detach_sc: close\n"));
    681 		/* mux device is open, so close multiplexee */
    682 		(void)me->me_ops->dclose(me);
    683 	}
    684 
    685 	CIRCLEQ_REMOVE(&sc->sc_cld, me, me_next);
    686 	me->me_parent = NULL;
    687 
    688 	DPRINTF(("wsmux_detach_sc: done sc=%p\n", sc));
    689 }
    690 
    691 /*
    692  * Display ioctl() of a mux via the parent mux.
    693  */
    694 int
    695 wsmux_do_displayioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
    696 		      struct proc *p)
    697 {
    698 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    699 	struct wsevsrc *me;
    700 	int error, ok;
    701 
    702 	DPRINTF(("wsmux_displayioctl: %s: sc=%p, cmd=%08lx\n",
    703 		 sc->sc_base.me_dv.dv_xname, sc, cmd));
    704 
    705 #ifdef WSDISPLAY_COMPAT_RAWKBD
    706 	if (cmd == WSKBDIO_SETMODE) {
    707 		sc->sc_rawkbd = *(int *)data;
    708 		DPRINTF(("wsmux_displayioctl: rawkbd = %d\n", sc->sc_rawkbd));
    709 	}
    710 #endif
    711 
    712 	/*
    713 	 * Return 0 if any of the ioctl() succeeds, otherwise the last error.
    714 	 * Return -1 if no mux component accepts the ioctl.
    715 	 */
    716 	error = -1;
    717 	ok = 0;
    718 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    719 		DPRINTF(("wsmux_displayioctl: me=%p\n", me));
    720 #ifdef DIAGNOSTIC
    721 		if (me->me_parent != sc) {
    722 			printf("wsmux_displayioctl: bad child %p\n", me);
    723 			continue;
    724 		}
    725 #endif
    726 		if (me->me_ops->ddispioctl != NULL) {
    727 			error = wsevsrc_display_ioctl(me, cmd, data, flag, p);
    728 			DPRINTF(("wsmux_displayioctl: me=%p dev=%s ==> %d\n",
    729 				 me, me->me_dv.dv_xname, error));
    730 			if (!error)
    731 				ok = 1;
    732 		}
    733 	}
    734 	if (ok)
    735 		error = 0;
    736 
    737 	return (error);
    738 }
    739 
    740 #if NWSDISPLAY > 0
    741 /*
    742  * Set display of a mux via the parent mux.
    743  */
    744 int
    745 wsmux_set_display(struct device *dv, struct wsevsrc *ame)
    746 {
    747 	struct wsmux_softc *muxsc = (struct wsmux_softc *)ame;
    748 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    749 	struct wsmux_softc *nsc = muxsc ? sc : NULL;
    750 	struct device *displaydv = muxsc ? muxsc->sc_base.me_dispdv : NULL;
    751 	struct device *odisplaydv;
    752 	struct wsevsrc *me;
    753 	int error, ok;
    754 
    755 	DPRINTF(("wsmux_set_display: %s: displaydv=%p\n",
    756 		 sc->sc_base.me_dv.dv_xname, displaydv));
    757 
    758 	if (displaydv != NULL) {
    759 		if (sc->sc_base.me_dispdv != NULL)
    760 			return (EBUSY);
    761 	} else {
    762 		if (sc->sc_base.me_dispdv == NULL)
    763 			return (ENXIO);
    764 	}
    765 
    766 	odisplaydv = sc->sc_base.me_dispdv;
    767 	sc->sc_base.me_dispdv = displaydv;
    768 
    769 	if (displaydv)
    770 		printf("%s: connecting to %s\n",
    771 		       sc->sc_base.me_dv.dv_xname, displaydv->dv_xname);
    772 	ok = 0;
    773 	error = 0;
    774 	CIRCLEQ_FOREACH(me, &sc->sc_cld,me_next) {
    775 #ifdef DIAGNOSTIC
    776 		if (me->me_parent != sc) {
    777 			printf("wsmux_set_display: bad child parent %p\n", me);
    778 			continue;
    779 		}
    780 #endif
    781 		if (me->me_ops->dsetdisplay != NULL) {
    782 			error = wsevsrc_set_display(me, &nsc->sc_base);
    783 			DPRINTF(("wsmux_set_display: m=%p dev=%s error=%d\n",
    784 				 me, me->me_dv.dv_xname, error));
    785 			if (!error) {
    786 				ok = 1;
    787 #ifdef WSDISPLAY_COMPAT_RAWKBD
    788 				DPRINTF(("wsmux_set_display: on %s set rawkbd=%d\n",
    789 					 me->me_dv.dv_xname, sc->sc_rawkbd));
    790 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
    791 						    &sc->sc_rawkbd, 0, 0);
    792 #endif
    793 			}
    794 		}
    795 	}
    796 	if (ok)
    797 		error = 0;
    798 
    799 	if (displaydv == NULL)
    800 		printf("%s: disconnecting from %s\n",
    801 		       sc->sc_base.me_dv.dv_xname, odisplaydv->dv_xname);
    802 
    803 	return (error);
    804 }
    805 #endif /* NWSDISPLAY > 0 */
    806