Home | History | Annotate | Line # | Download | only in wscons
wsmux.c revision 1.10
      1 /*	$NetBSD: wsmux.c,v 1.10 2001/10/13 13:36:02 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 #include "wsmux.h"
     40 #include "wsdisplay.h"
     41 #include "wskbd.h"
     42 
     43 #if NWSMUX > 0 || (NWSDISPLAY > 0 && NWSKBD > 0)
     44 
     45 /*
     46  * wscons mux device.
     47  *
     48  * The mux device is a collection of real mice and keyboards and acts as
     49  * a merge point for all the events from the different real devices.
     50  */
     51 
     52 #include <sys/param.h>
     53 #include <sys/conf.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/fcntl.h>
     56 #include <sys/kernel.h>
     57 #include <sys/malloc.h>
     58 #include <sys/proc.h>
     59 #include <sys/queue.h>
     60 #include <sys/syslog.h>
     61 #include <sys/systm.h>
     62 #include <sys/tty.h>
     63 #include <sys/signalvar.h>
     64 #include <sys/device.h>
     65 
     66 #include "opt_wsdisplay_compat.h"
     67 
     68 #include <dev/wscons/wsconsio.h>
     69 #include <dev/wscons/wseventvar.h>
     70 #include <dev/wscons/wscons_callbacks.h>
     71 #include <dev/wscons/wsmuxvar.h>
     72 
     73 #ifdef WSMUX_DEBUG
     74 #define DPRINTF(x)	if (wsmuxdebug) printf x
     75 int	wsmuxdebug = 0;
     76 #else
     77 #define DPRINTF(x)
     78 #endif
     79 
     80 struct wsplink {
     81 	LIST_ENTRY(wsplink) next;
     82 	int type;
     83 	struct wsmux_softc *mux; /* our mux device */
     84 	/* The rest of the fields reflect a value in the multiplexee. */
     85 	struct device *sc;	/* softc */
     86 	struct wseventvar *sc_mevents; /* event var */
     87 	struct wsmux_softc **sc_muxp; /* pointer to us */
     88 	struct wsmuxops *sc_ops;
     89 };
     90 
     91 int wsmuxdoclose __P((struct device *, int, int, struct proc *));
     92 int wsmux_set_display __P((struct device *, struct wsmux_softc *));
     93 
     94 #if NWSMUX > 0
     95 cdev_decl(wsmux);
     96 
     97 void wsmuxattach __P((int));
     98 
     99 struct wsmuxops wsmux_muxops = {
    100 	wsmuxopen, wsmuxdoclose, wsmuxdoioctl, wsmux_displayioctl,
    101 	wsmux_set_display
    102 };
    103 
    104 void wsmux_setmax __P((int n));
    105 
    106 int nwsmux = 0;
    107 struct wsmux_softc **wsmuxdevs;
    108 
    109 void
    110 wsmux_setmax(n)
    111 	int n;
    112 {
    113 	int i;
    114 
    115 	if (n >= nwsmux) {
    116 		i = nwsmux;
    117 		nwsmux = n + 1;
    118 		if (i != 0)
    119 			wsmuxdevs = realloc(wsmuxdevs,
    120 					    nwsmux * sizeof (*wsmuxdevs),
    121 					    M_DEVBUF, M_NOWAIT);
    122 		else
    123 			wsmuxdevs = malloc(nwsmux * sizeof (*wsmuxdevs),
    124 					   M_DEVBUF, M_NOWAIT);
    125 		if (wsmuxdevs == 0)
    126 			panic("wsmux_setmax: no memory\n");
    127 		for (; i < nwsmux; i++)
    128 			wsmuxdevs[i] = 0;
    129 	}
    130 }
    131 
    132 /* From upper level */
    133 void
    134 wsmuxattach(n)
    135 	int n;
    136 {
    137 }
    138 
    139 /* Return mux n, create if necessary */
    140 struct wsmux_softc *
    141 wsmux_getmux(n)
    142 	int n;
    143 {
    144 	struct wsmux_softc *sc;
    145 
    146 	wsmux_setmax(n);
    147 	sc = wsmuxdevs[n];
    148 	if (sc == 0) {
    149 		sc = wsmux_create("wsmux", n);
    150 		if (sc == 0) {
    151 			printf("wsmux: attach out of memory\n");
    152 			return (NULL);
    153 		}
    154 		wsmuxdevs[n] = sc;
    155 	}
    156 	return (sc);
    157 }
    158 
    159 /* From mouse or keyboard. */
    160 void
    161 wsmux_attach(n, type, dsc, ev, psp, ops)
    162 	int n;
    163 	int type;
    164         struct device *dsc;
    165 	struct wseventvar *ev;
    166 	struct wsmux_softc **psp;
    167 	struct wsmuxops *ops;
    168 {
    169 	struct wsmux_softc *sc;
    170 	int error;
    171 
    172 	DPRINTF(("wsmux_attach: n=%d\n", n));
    173 	sc = wsmux_getmux(n);
    174 	error = wsmux_attach_sc(sc, type, dsc, ev, psp, ops);
    175 	if (error)
    176 		printf("wsmux_attach: error=%d\n", error);
    177 }
    178 
    179 /* From mouse or keyboard. */
    180 void
    181 wsmux_detach(n, dsc)
    182 	int n;
    183         struct device *dsc;
    184 {
    185 #ifdef DIAGNOSTIC
    186 	int error;
    187 
    188 	if (n >= nwsmux || n < 0) {
    189 		printf("wsmux_detach: detach is out of range\n");
    190 		return;
    191 	}
    192 	if ((error = wsmux_detach_sc(wsmuxdevs[n], dsc)))
    193 		printf("wsmux_detach: error=%d\n", error);
    194 #else
    195 	(void)wsmux_detach_sc(wsmuxdevs[n], dsc);
    196 #endif
    197 }
    198 
    199 int
    200 wsmuxopen(dev, flags, mode, p)
    201 	dev_t dev;
    202 	int flags, mode;
    203 	struct proc *p;
    204 {
    205 	struct wsmux_softc *sc;
    206 	struct wsplink *m;
    207 	int unit, error, nopen, lasterror;
    208 
    209 	unit = minor(dev);
    210 	if (unit >= nwsmux ||	/* make sure it was attached */
    211 	    (sc = wsmuxdevs[unit]) == NULL)
    212 		return (ENXIO);
    213 
    214 	DPRINTF(("wsmuxopen: %s: sc=%p\n", sc->sc_dv.dv_xname, sc));
    215 	if (!(flags & FREAD)) {
    216 		/* Not opening for read, only ioctl is available. */
    217 		return (0);
    218 	}
    219 
    220 	if (sc->sc_events.io)
    221 		return (EBUSY);
    222 
    223 	sc->sc_events.io = p;
    224 	sc->sc_flags = flags;
    225 	sc->sc_mode = mode;
    226 	sc->sc_p = p;
    227 	wsevent_init(&sc->sc_events);		/* may cause sleep */
    228 
    229 	nopen = 0;
    230 	lasterror = 0;
    231 	for (m = LIST_FIRST(&sc->sc_reals); m; m = LIST_NEXT(m, next)) {
    232 		if (!m->sc_mevents->io && !*m->sc_muxp) {
    233 			DPRINTF(("wsmuxopen: %s: m=%p dev=%s\n",
    234 				 sc->sc_dv.dv_xname, m, m->sc->dv_xname));
    235 			error = m->sc_ops->dopen(makedev(0, m->sc->dv_unit),
    236 						 flags, mode, p);
    237 			if (error) {
    238 				/* Ignore opens that fail */
    239 				lasterror = error;
    240 				DPRINTF(("wsmuxopen: open failed %d\n",
    241 					 error));
    242 			} else {
    243 				nopen++;
    244 				*m->sc_muxp = sc;
    245 			}
    246 		}
    247 	}
    248 
    249 	if (nopen == 0 && lasterror != 0) {
    250 		wsevent_fini(&sc->sc_events);
    251 		sc->sc_events.io = NULL;
    252 		return (lasterror);
    253 	}
    254 
    255 	return (0);
    256 }
    257 
    258 int
    259 wsmuxclose(dev, flags, mode, p)
    260 	dev_t dev;
    261 	int flags, mode;
    262 	struct proc *p;
    263 {
    264 	return wsmuxdoclose(&wsmuxdevs[minor(dev)]->sc_dv, flags, mode, p);
    265 }
    266 
    267 int
    268 wsmuxread(dev, uio, flags)
    269 	dev_t dev;
    270 	struct uio *uio;
    271 	int flags;
    272 {
    273 	struct wsmux_softc *sc = wsmuxdevs[minor(dev)];
    274 
    275 	if (!sc->sc_events.io)
    276 		return (EACCES);
    277 
    278 	return (wsevent_read(&sc->sc_events, uio, flags));
    279 }
    280 
    281 int
    282 wsmuxioctl(dev, cmd, data, flag, p)
    283 	dev_t dev;
    284 	u_long cmd;
    285 	caddr_t data;
    286 	int flag;
    287 	struct proc *p;
    288 {
    289 	return wsmuxdoioctl(&wsmuxdevs[minor(dev)]->sc_dv, cmd, data, flag, p);
    290 }
    291 
    292 int
    293 wsmuxpoll(dev, events, p)
    294 	dev_t dev;
    295 	int events;
    296 	struct proc *p;
    297 {
    298 	struct wsmux_softc *sc = wsmuxdevs[minor(dev)];
    299 
    300 	if (!sc->sc_events.io)
    301 		return (EACCES);
    302 
    303 	return (wsevent_poll(&sc->sc_events, events, p));
    304 }
    305 
    306 int
    307 wsmux_add_mux(unit, muxsc)
    308 	int unit;
    309 	struct wsmux_softc *muxsc;
    310 {
    311 	struct wsmux_softc *sc, *m;
    312 
    313 	if (unit < 0 || unit >= nwsmux || (sc = wsmuxdevs[unit]) == NULL)
    314 		return (ENXIO);
    315 
    316 	DPRINTF(("wsmux_add_mux: %s(%p) to %s(%p)\n", sc->sc_dv.dv_xname, sc,
    317 		 muxsc->sc_dv.dv_xname, muxsc));
    318 
    319 	if (sc->sc_mux || sc->sc_events.io)
    320 		return (EBUSY);
    321 
    322 	/* The mux we are adding must not be an ancestor of itself. */
    323 	for (m = muxsc; m; m = m->sc_mux)
    324 		if (m == sc)
    325 			return (EINVAL);
    326 
    327 	return (wsmux_attach_sc(muxsc, WSMUX_MUX, &sc->sc_dv, &sc->sc_events,
    328 				&sc->sc_mux, &wsmux_muxops));
    329 }
    330 
    331 int
    332 wsmux_rem_mux(unit, muxsc)
    333 	int unit;
    334 	struct wsmux_softc *muxsc;
    335 {
    336 	struct wsmux_softc *sc;
    337 
    338 	if (unit < 0 || unit >= nwsmux || (sc = wsmuxdevs[unit]) == NULL)
    339 		return (ENXIO);
    340 
    341 	DPRINTF(("wsmux_rem_mux: %s from %s\n", sc->sc_dv.dv_xname,
    342 		 muxsc->sc_dv.dv_xname));
    343 
    344 	return (wsmux_detach_sc(muxsc, &sc->sc_dv));
    345 }
    346 
    347 #endif /* NWSMUX > 0 */
    348 
    349 struct wsmux_softc *
    350 wsmux_create(name, unit)
    351 	const char *name;
    352 	int unit;
    353 {
    354 	struct wsmux_softc *sc;
    355 
    356 	DPRINTF(("wsmux_create: allocating\n"));
    357 	sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT);
    358 	if (sc == NULL)
    359 		return (NULL);
    360 	memset(sc, 0, sizeof *sc);
    361 	LIST_INIT(&sc->sc_reals);
    362 	snprintf(sc->sc_dv.dv_xname, sizeof sc->sc_dv.dv_xname,
    363 		 "%s%d", name, unit);
    364 	sc->sc_dv.dv_unit = unit;
    365 	return (sc);
    366 }
    367 
    368 int
    369 wsmux_attach_sc(sc, type, dsc, ev, psp, ops)
    370 	struct wsmux_softc *sc;
    371 	int type;
    372         struct device *dsc;
    373 	struct wseventvar *ev;
    374 	struct wsmux_softc **psp;
    375 	struct wsmuxops *ops;
    376 {
    377 	struct wsplink *m;
    378 	int error;
    379 
    380 	DPRINTF(("wsmux_attach_sc: %s: type=%d dsc=%p, *psp=%p\n",
    381 		 sc->sc_dv.dv_xname, type, dsc, *psp));
    382 	m = malloc(sizeof *m, M_DEVBUF, M_NOWAIT);
    383 	if (m == NULL)
    384 		return (ENOMEM);
    385 	m->type = type;
    386 	m->mux = sc;
    387 	m->sc = dsc;
    388 	m->sc_mevents = ev;
    389 	m->sc_muxp = psp;
    390 	m->sc_ops = ops;
    391 	LIST_INSERT_HEAD(&sc->sc_reals, m, next);
    392 
    393 	if (sc->sc_displaydv) {
    394 		/* This is a display mux, so attach the new device to it. */
    395 		DPRINTF(("wsmux_attach_sc: %s: set display %p\n",
    396 			 sc->sc_dv.dv_xname, sc->sc_displaydv));
    397 		error = 0;
    398 		if (m->sc_ops->dsetdisplay) {
    399 			error = m->sc_ops->dsetdisplay(m->sc, sc);
    400 			/* Ignore that the console already has a display. */
    401 			if (error == EBUSY)
    402 				error = 0;
    403 			if (!error) {
    404 				*m->sc_muxp = sc;
    405 #ifdef WSDISPLAY_COMPAT_RAWKBD
    406 				DPRINTF(("wsmux_attach_sc: on %s set rawkbd=%d\n",
    407 					 m->sc->dv_xname, sc->sc_rawkbd));
    408 				(void)m->sc_ops->dioctl(m->sc,
    409 					     WSKBDIO_SETMODE,
    410 					     (caddr_t)&sc->sc_rawkbd,
    411 					     0, 0);
    412 #endif
    413 			}
    414 		}
    415 	} else if (sc->sc_events.io) {
    416 		/* Mux is open, so open the new subdevice */
    417 		DPRINTF(("wsmux_attach_sc: %s: calling open of %s\n",
    418 			 sc->sc_dv.dv_xname, m->sc->dv_xname));
    419 		/* mux already open, join in */
    420 		error = m->sc_ops->dopen(makedev(0, m->sc->dv_unit),
    421 					 sc->sc_flags, sc->sc_mode, sc->sc_p);
    422 		if (!error)
    423 			*m->sc_muxp = sc;
    424 	} else {
    425 		DPRINTF(("wsmux_attach_sc: %s not open\n",
    426 			 sc->sc_dv.dv_xname));
    427 		error = 0;
    428 	}
    429 	DPRINTF(("wsmux_attach_sc: done sc=%p psp=%p *psp=%p\n",
    430 		 sc, psp, *psp));
    431 
    432 	return (error);
    433 }
    434 
    435 int
    436 wsmux_detach_sc(sc, dsc)
    437 	struct wsmux_softc *sc;
    438         struct device *dsc;
    439 {
    440 	struct wsplink *m;
    441 	int error = 0;
    442 
    443 	DPRINTF(("wsmux_detach_sc: %s: dsc=%p\n", sc->sc_dv.dv_xname, dsc));
    444 #ifdef DIAGNOSTIC
    445 	if (sc == 0) {
    446 		printf("wsmux_detach_sc: not allocated\n");
    447 		return (ENXIO);
    448 	}
    449 #endif
    450 
    451 	for (m = LIST_FIRST(&sc->sc_reals); m; m = LIST_NEXT(m, next)) {
    452 		if (m->sc == dsc)
    453 			break;
    454 	}
    455 #ifdef DIAGNOSTIC
    456 	if (!m) {
    457 		printf("wsmux_detach_sc: not found\n");
    458 		return (ENXIO);
    459 	}
    460 #endif
    461 	if (sc->sc_displaydv) {
    462 		if (m->sc_ops->dsetdisplay)
    463 			error = m->sc_ops->dsetdisplay(m->sc, 0);
    464 		if (error)
    465 			return (error);
    466 		*m->sc_muxp = 0;
    467 	} else if (*m->sc_muxp) {
    468 		DPRINTF(("wsmux_detach_sc: close\n"));
    469 		/* mux device is open, so close multiplexee */
    470 		m->sc_ops->dclose(m->sc, FREAD, 0, 0);
    471 		*m->sc_muxp = 0;
    472 	}
    473 
    474 	LIST_REMOVE(m, next);
    475 
    476 	free(m, M_DEVBUF);
    477 	DPRINTF(("wsmux_detach_sc: done sc=%p\n", sc));
    478 	return (0);
    479 }
    480 
    481 int
    482 wsmuxdoclose(dv, flags, mode, p)
    483 	struct device *dv;
    484 	int flags, mode;
    485 	struct proc *p;
    486 {
    487 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    488 	struct wsplink *m;
    489 
    490 	DPRINTF(("wsmuxclose: %s: sc=%p\n", sc->sc_dv.dv_xname, sc));
    491 	if (!(flags & FREAD)) {
    492 		/* Nothing to do, because open didn't do anything. */
    493 		return (0);
    494 	}
    495 
    496 	for (m = LIST_FIRST(&sc->sc_reals); m; m = LIST_NEXT(m, next)) {
    497 		if (*m->sc_muxp == sc) {
    498 			DPRINTF(("wsmuxclose %s: m=%p dev=%s\n",
    499 				 sc->sc_dv.dv_xname, m, m->sc->dv_xname));
    500 			m->sc_ops->dclose(m->sc, flags, mode, p);
    501 			*m->sc_muxp = 0;
    502 		}
    503 	}
    504 
    505 	wsevent_fini(&sc->sc_events);
    506 	sc->sc_events.io = NULL;
    507 
    508 	return (0);
    509 }
    510 
    511 int
    512 wsmuxdoioctl(dv, cmd, data, flag, p)
    513 	struct device *dv;
    514 	u_long cmd;
    515 	caddr_t data;
    516 	int flag;
    517 	struct proc *p;
    518 {
    519 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    520 	struct wsplink *m;
    521 	int error, ok;
    522 	int s, put, get, n;
    523 	struct wseventvar *evar;
    524 	struct wscons_event *ev;
    525 	struct timeval xxxtime;
    526 	struct wsmux_device_list *l;
    527 
    528 	DPRINTF(("wsmuxdoioctl: %s: sc=%p, cmd=%08lx\n",
    529 		 sc->sc_dv.dv_xname, sc, cmd));
    530 
    531 	switch (cmd) {
    532 	case WSMUX_INJECTEVENT:
    533 		/* Inject an event, e.g., from moused. */
    534 		if (!sc->sc_events.io)
    535 			return (EACCES);
    536 
    537 		evar = &sc->sc_events;
    538 		s = spltty();
    539 		get = evar->get;
    540 		put = evar->put;
    541 		if (++put % WSEVENT_QSIZE == get) {
    542 			put--;
    543 			splx(s);
    544 			return (ENOSPC);
    545 		}
    546 		if (put >= WSEVENT_QSIZE)
    547 			put = 0;
    548 		ev = &evar->q[put];
    549 		*ev = *(struct wscons_event *)data;
    550 		microtime(&xxxtime);
    551 		TIMEVAL_TO_TIMESPEC(&xxxtime, &ev->time);
    552 		evar->put = put;
    553 		WSEVENT_WAKEUP(evar);
    554 		splx(s);
    555 		return (0);
    556 	case WSMUX_ADD_DEVICE:
    557 #define d ((struct wsmux_device *)data)
    558 		switch (d->type) {
    559 #if NWSMOUSE > 0
    560 		case WSMUX_MOUSE:
    561 			return (wsmouse_add_mux(d->idx, sc));
    562 #endif
    563 #if NWSKBD > 0
    564 		case WSMUX_KBD:
    565 			return (wskbd_add_mux(d->idx, sc));
    566 #endif
    567 #if NWSMUX > 0
    568 		case WSMUX_MUX:
    569 			return (wsmux_add_mux(d->idx, sc));
    570 #endif
    571 		default:
    572 			return (EINVAL);
    573 		}
    574 	case WSMUX_REMOVE_DEVICE:
    575 		switch (d->type) {
    576 #if NWSMOUSE > 0
    577 		case WSMUX_MOUSE:
    578 			return (wsmouse_rem_mux(d->idx, sc));
    579 #endif
    580 #if NWSKBD > 0
    581 		case WSMUX_KBD:
    582 			return (wskbd_rem_mux(d->idx, sc));
    583 #endif
    584 #if NWSMUX > 0
    585 		case WSMUX_MUX:
    586 			return (wsmux_rem_mux(d->idx, sc));
    587 #endif
    588 		default:
    589 			return (EINVAL);
    590 		}
    591 #undef d
    592 	case WSMUX_LIST_DEVICES:
    593 		l = (struct wsmux_device_list *)data;
    594 		for (n = 0, m = LIST_FIRST(&sc->sc_reals);
    595 		     n < WSMUX_MAXDEV && m != NULL;
    596 		     m = LIST_NEXT(m, next)) {
    597 			l->devices[n].type = m->type;
    598 			l->devices[n].idx = m->sc->dv_unit;
    599 			n++;
    600 		}
    601 		l->ndevices = n;
    602 		return (0);
    603 #ifdef WSDISPLAY_COMPAT_RAWKBD
    604 	case WSKBDIO_SETMODE:
    605 		sc->sc_rawkbd = *(int *)data;
    606 		DPRINTF(("wsmuxdoioctl: save rawkbd = %d\n", sc->sc_rawkbd));
    607 		break;
    608 #endif
    609 	case FIOASYNC:
    610 		sc->sc_events.async = *(int *)data != 0;
    611 		return (0);
    612 	case TIOCSPGRP:
    613 		if (*(int *)data != sc->sc_events.io->p_pgid)
    614 			return (EPERM);
    615 		return (0);
    616 	default:
    617 		break;
    618 	}
    619 
    620 	if (sc->sc_events.io == NULL && sc->sc_displaydv == NULL)
    621 		return (EACCES);
    622 
    623 	/* Return 0 if any of the ioctl() succeeds, otherwise the last error */
    624 	error = 0;
    625 	ok = 0;
    626 	for (m = LIST_FIRST(&sc->sc_reals); m; m = LIST_NEXT(m, next)) {
    627 		DPRINTF(("wsmuxdoioctl: m=%p *m->sc_muxp=%p sc=%p\n",
    628 			 m, *m->sc_muxp, sc));
    629 		if (*m->sc_muxp == sc) {
    630 			DPRINTF(("wsmuxdoioctl: %s: m=%p dev=%s\n",
    631 				 sc->sc_dv.dv_xname, m, m->sc->dv_xname));
    632 			error = m->sc_ops->dioctl(m->sc, cmd, data, flag, p);
    633 			if (!error)
    634 				ok = 1;
    635 		}
    636 	}
    637 	if (ok)
    638 		error = 0;
    639 
    640 	return (error);
    641 }
    642 
    643 int
    644 wsmux_displayioctl(dv, cmd, data, flag, p)
    645 	struct device *dv;
    646 	u_long cmd;
    647 	caddr_t data;
    648 	int flag;
    649 	struct proc *p;
    650 {
    651 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    652 	struct wsplink *m;
    653 	int error, ok;
    654 
    655 	DPRINTF(("wsmux_displayioctl: %s: sc=%p, cmd=%08lx\n",
    656 		 sc->sc_dv.dv_xname, sc, cmd));
    657 
    658 #ifdef WSDISPLAY_COMPAT_RAWKBD
    659 	if (cmd == WSKBDIO_SETMODE) {
    660 		sc->sc_rawkbd = *(int *)data;
    661 		DPRINTF(("wsmux_displayioctl: rawkbd = %d\n", sc->sc_rawkbd));
    662 	}
    663 #endif
    664 
    665 	/*
    666 	 * Return 0 if any of the ioctl() succeeds, otherwise the last error.
    667 	 * Return -1 if no mux component accepts the ioctl.
    668 	 */
    669 	error = -1;
    670 	ok = 0;
    671 	for (m = LIST_FIRST(&sc->sc_reals); m; m = LIST_NEXT(m, next)) {
    672 		DPRINTF(("wsmux_displayioctl: m=%p sc=%p sc_muxp=%p\n",
    673 			 m, sc, *m->sc_muxp));
    674 		if (m->sc_ops->ddispioctl && *m->sc_muxp == sc) {
    675 			error = m->sc_ops->ddispioctl(m->sc, cmd, data,
    676 						      flag, p);
    677 			DPRINTF(("wsmux_displayioctl: m=%p dev=%s ==> %d\n",
    678 				 m, m->sc->dv_xname, error));
    679 			if (!error)
    680 				ok = 1;
    681 		}
    682 	}
    683 	if (ok)
    684 		error = 0;
    685 
    686 	return (error);
    687 }
    688 
    689 int
    690 wsmux_set_display(dv, muxsc)
    691 	struct device *dv;
    692 	struct wsmux_softc *muxsc;
    693 {
    694 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    695 	struct wsmux_softc *nsc = muxsc ? sc : 0;
    696 	struct device *displaydv = muxsc ? muxsc->sc_displaydv : 0;
    697 	struct device *odisplaydv;
    698 	struct wsplink *m;
    699 	int error, ok;
    700 
    701 	DPRINTF(("wsmux_set_display: %s: displaydv=%p\n",
    702 		 sc->sc_dv.dv_xname, displaydv));
    703 
    704 	if (displaydv) {
    705 		if (sc->sc_displaydv)
    706 			return (EBUSY);
    707 	} else {
    708 		if (sc->sc_displaydv == NULL)
    709 			return (ENXIO);
    710 	}
    711 
    712 	odisplaydv = sc->sc_displaydv;
    713 	sc->sc_displaydv = displaydv;
    714 
    715 	if (displaydv)
    716 		printf("%s: connecting to %s\n",
    717 		       sc->sc_dv.dv_xname, displaydv->dv_xname);
    718 	ok = 0;
    719 	error = 0;
    720 	for (m = LIST_FIRST(&sc->sc_reals); m; m = LIST_NEXT(m, next)) {
    721 		if (m->sc_ops->dsetdisplay &&
    722 		    (nsc ? m->sc_mevents->io == 0 && *m->sc_muxp == 0 :
    723 		           *m->sc_muxp == sc)) {
    724 			error = m->sc_ops->dsetdisplay(m->sc, nsc);
    725 			DPRINTF(("wsmux_set_display: m=%p dev=%s error=%d\n",
    726 				 m, m->sc->dv_xname, error));
    727 			if (!error) {
    728 				ok = 1;
    729 				*m->sc_muxp = nsc;
    730 #ifdef WSDISPLAY_COMPAT_RAWKBD
    731 				DPRINTF(("wsmux_set_display: on %s set rawkbd=%d\n",
    732 					 m->sc->dv_xname, sc->sc_rawkbd));
    733 				(void)m->sc_ops->dioctl(m->sc,
    734 					     WSKBDIO_SETMODE,
    735 					     (caddr_t)&sc->sc_rawkbd,
    736 					     0, 0);
    737 #endif
    738 			}
    739 		}
    740 	}
    741 	if (ok)
    742 		error = 0;
    743 
    744 	if (displaydv == NULL)
    745 		printf("%s: disconnecting from %s\n",
    746 		       sc->sc_dv.dv_xname, odisplaydv->dv_xname);
    747 
    748 	return (error);
    749 }
    750 
    751 #endif /* NWSMUX > 0 || (NWSDISPLAY > 0 && NWSKBD > 0) */
    752