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