Home | History | Annotate | Line # | Download | only in wscons
wsmux.c revision 1.22
      1 /*	$NetBSD: wsmux.c,v 1.22 2001/11/02 13:02:20 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 		DPRINTF(("wsmuxopen: detach\n"));
    207 		wsmux_detach_sc(&sc->sc_base);
    208 	}
    209 
    210 	if (sc->sc_base.me_evp != NULL)
    211 		/* Already open. */
    212 		return (EBUSY);
    213 
    214 	evar = &sc->sc_base.me_evar;
    215 	wsevent_init(evar);
    216 	evar->io = p;
    217 #ifdef WSDISPLAY_COMPAT_RAWKBD
    218 	sc->sc_rawkbd = 0;
    219 #endif
    220 
    221 	wsmux_do_open(sc, evar);
    222 
    223 	return (0);
    224 }
    225 
    226 /*
    227  * Open of a mux via the parent mux.
    228  */
    229 int
    230 wsmux_mux_open(struct wsevsrc *me, struct wseventvar *evar)
    231 {
    232 	struct wsmux_softc *sc = (struct wsmux_softc *)me;
    233 
    234 #ifdef DIAGNOSTIC
    235 	if (sc->sc_base.me_evp != NULL) {
    236 		printf("wsmux_mux_open: busy\n");
    237 		return (EBUSY);
    238 	}
    239 	if (sc->sc_base.me_parent == NULL) {
    240 		printf("wsmux_mux_open: no parent\n");
    241 		return (EINVAL);
    242 	}
    243 #endif
    244 
    245 	wsmux_do_open(sc, evar);
    246 
    247 	return (0);
    248 }
    249 
    250 /* Common part of opening a mux. */
    251 void
    252 wsmux_do_open(struct wsmux_softc *sc, struct wseventvar *evar)
    253 {
    254 	struct wsevsrc *me;
    255 
    256 	sc->sc_base.me_evp = evar; /* remember event variable, mark as open */
    257 
    258 	/* Open all children. */
    259 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    260 		DPRINTF(("wsmuxopen: %s: m=%p dev=%s\n",
    261 			 sc->sc_base.me_dv.dv_xname, me, me->me_dv.dv_xname));
    262 #ifdef DIAGNOSTIC
    263 		if (me->me_evp != NULL) {
    264 			printf("wsmuxopen: dev already in use\n");
    265 			continue;
    266 		}
    267 		if (me->me_parent != sc) {
    268 			printf("wsmux_do_open: bad child=%p\n", me);
    269 			continue;
    270 		}
    271 		{
    272 		int error = wsevsrc_open(me, evar);
    273 		if (error) {
    274 			DPRINTF(("wsmuxopen: open failed %d\n", error));
    275 		}
    276 		}
    277 #else
    278 		/* ignore errors, failing children will not be marked open */
    279 		(void)wsevsrc_open(me, evar);
    280 #endif
    281 	}
    282 }
    283 
    284 /*
    285  * close() of the pseudo device from device table.
    286  */
    287 int
    288 wsmuxclose(dev_t dev, int flags, int mode, struct proc *p)
    289 {
    290 	int minr = minor(dev);
    291 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    292 	struct wseventvar *evar = sc->sc_base.me_evp;
    293 
    294 	if (WSMUXCTL(minr))
    295 		/* control device */
    296 		return (0);
    297 	if (evar == NULL)
    298 		/* Not open for read */
    299 		return (0);
    300 
    301 	sc->sc_base.me_evp = NULL;
    302 	wsevent_fini(evar);
    303 	wsmux_do_close(sc);
    304 	return (0);
    305 }
    306 
    307 /*
    308  * Close of a mux via the parent mux.
    309  */
    310 int
    311 wsmux_mux_close(struct wsevsrc *me)
    312 {
    313 	me->me_evp = NULL;
    314 	wsmux_do_close((struct wsmux_softc *)me);
    315 	return (0);
    316 }
    317 
    318 /* Common part of closing a mux. */
    319 void
    320 wsmux_do_close(struct wsmux_softc *sc)
    321 {
    322 	struct wsevsrc *me;
    323 
    324 	DPRINTF(("wsmuxclose: %s: sc=%p\n", sc->sc_base.me_dv.dv_xname, sc));
    325 
    326 	/* Close all the children. */
    327 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    328 		DPRINTF(("wsmuxclose %s: m=%p dev=%s\n",
    329 			 sc->sc_base.me_dv.dv_xname, me, me->me_dv.dv_xname));
    330 #ifdef DIAGNOSTIC
    331 		if (me->me_parent != sc) {
    332 			printf("wsmuxclose: bad child=%p\n", me);
    333 			continue;
    334 		}
    335 #endif
    336 		(void)wsevsrc_close(me);
    337 		me->me_evp = NULL;
    338 	}
    339 }
    340 
    341 /*
    342  * read() of the pseudo device from device table.
    343  */
    344 int
    345 wsmuxread(dev_t dev, struct uio *uio, int flags)
    346 {
    347 	int minr = minor(dev);
    348 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    349 	struct wseventvar *evar;
    350 	int error;
    351 
    352 	if (WSMUXCTL(minr)) {
    353 		/* control device */
    354 		return (EINVAL);
    355 	}
    356 
    357 	evar = sc->sc_base.me_evp;
    358 	if (evar == NULL) {
    359 #ifdef DIAGNOSTIC
    360 		/* XXX can we get here? */
    361 		printf("wsmuxread: not open\n");
    362 #endif
    363 		return (EINVAL);
    364 	}
    365 
    366 	DPRINTFN(5,("wsmuxread: %s event read evar=%p\n",
    367 		    sc->sc_base.me_dv.dv_xname, evar));
    368 	error = wsevent_read(evar, uio, flags);
    369 	DPRINTFN(5,("wsmuxread: %s event read ==> error=%d\n",
    370 		    sc->sc_base.me_dv.dv_xname, error));
    371 	return (error);
    372 }
    373 
    374 /*
    375  * ioctl of the pseudo device from device table.
    376  */
    377 int
    378 wsmuxioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    379 {
    380 	int u = WSMUXDEV(minor(dev));
    381 
    382 	return wsmux_do_ioctl(&wsmuxdevs[u]->sc_base.me_dv, cmd, data, flag, p);
    383 }
    384 
    385 /*
    386  * ioctl of a mux via the parent mux, continuation of wsmuxioctl().
    387  */
    388 int
    389 wsmux_do_ioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
    390 	       struct proc *p)
    391 {
    392 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    393 	struct wsevsrc *me;
    394 	int error, ok;
    395 	int s, put, get, n;
    396 	struct wseventvar *evar;
    397 	struct wscons_event *ev;
    398 	struct timeval thistime;
    399 	struct wsmux_device_list *l;
    400 
    401 	DPRINTF(("wsmux_do_ioctl: %s: enter sc=%p, cmd=%08lx\n",
    402 		 sc->sc_base.me_dv.dv_xname, sc, cmd));
    403 
    404 	switch (cmd) {
    405 	case WSMUXIO_INJECTEVENT:
    406 		/* Inject an event, e.g., from moused. */
    407 		DPRINTF(("%s: inject\n", sc->sc_base.me_dv.dv_xname));
    408 
    409 		evar = sc->sc_base.me_evp;
    410 		if (evar == NULL) /* XXX is this an error? */
    411 			return (EACCES);
    412 
    413 		s = spltty();
    414 		get = evar->get;
    415 		put = evar->put;
    416 		ev = &evar->q[put];
    417 		if (++put % WSEVENT_QSIZE == get) {
    418 			put--;
    419 			splx(s);
    420 			return (ENOSPC);
    421 		}
    422 		if (put >= WSEVENT_QSIZE)
    423 			put = 0;
    424 		*ev = *(struct wscons_event *)data;
    425 		microtime(&thistime);
    426 		TIMEVAL_TO_TIMESPEC(&thistime, &ev->time);
    427 		evar->put = put;
    428 		WSEVENT_WAKEUP(evar);
    429 		splx(s);
    430 		return (0);
    431 	case WSMUXIO_ADD_DEVICE:
    432 #define d ((struct wsmux_device *)data)
    433 		DPRINTF(("%s: add type=%d, no=%d\n", sc->sc_base.me_dv.dv_xname,
    434 			 d->type, d->idx));
    435 		switch (d->type) {
    436 #if NWSMOUSE > 0
    437 		case WSMUX_MOUSE:
    438 			return (wsmouse_add_mux(d->idx, sc));
    439 #endif
    440 #if NWSKBD > 0
    441 		case WSMUX_KBD:
    442 			return (wskbd_add_mux(d->idx, sc));
    443 #endif
    444 		case WSMUX_MUX:
    445 			return (wsmux_add_mux(d->idx, sc));
    446 		default:
    447 			return (EINVAL);
    448 		}
    449 	case WSMUXIO_REMOVE_DEVICE:
    450 		DPRINTF(("%s: rem type=%d, no=%d\n", sc->sc_base.me_dv.dv_xname,
    451 			 d->type, d->idx));
    452 		/* Locate the device */
    453 		CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    454 			if (me->me_ops->type == d->type &&
    455 			    me->me_dv.dv_unit == d->idx) {
    456 				DPRINTF(("wsmux_do_ioctl: detach\n"));
    457 				wsmux_detach_sc(me);
    458 				return (0);
    459 			}
    460 		}
    461 		return (EINVAL);
    462 #undef d
    463 
    464 	case WSMUXIO_LIST_DEVICES:
    465 		DPRINTF(("%s: list\n", sc->sc_base.me_dv.dv_xname));
    466 		l = (struct wsmux_device_list *)data;
    467 		n = 0;
    468 		CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    469 			if (n >= WSMUX_MAXDEV)
    470 				break;
    471 			l->devices[n].type = me->me_ops->type;
    472 			l->devices[n].idx = me->me_dv.dv_unit;
    473 			n++;
    474 		}
    475 		l->ndevices = n;
    476 		return (0);
    477 #ifdef WSDISPLAY_COMPAT_RAWKBD
    478 	case WSKBDIO_SETMODE:
    479 		sc->sc_rawkbd = *(int *)data;
    480 		DPRINTF(("wsmux_do_ioctl: save rawkbd = %d\n", sc->sc_rawkbd));
    481 		break;
    482 #endif
    483 	case FIONBIO:
    484 		DPRINTF(("%s: FIONBIO\n", sc->sc_base.me_dv.dv_xname));
    485 		return (0);
    486 
    487 	case FIOASYNC:
    488 		DPRINTF(("%s: FIOASYNC\n", sc->sc_base.me_dv.dv_xname));
    489 		evar = sc->sc_base.me_evp;
    490 		if (evar == NULL)
    491 			return (EINVAL);
    492 		evar->async = *(int *)data != 0;
    493 		return (0);
    494 	case TIOCSPGRP:
    495 		DPRINTF(("%s: TIOCSPGRP\n", sc->sc_base.me_dv.dv_xname));
    496 		evar = sc->sc_base.me_evp;
    497 		if (evar == NULL)
    498 			return (EINVAL);
    499 		if (*(int *)data != evar->io->p_pgid)
    500 			return (EPERM);
    501 		return (0);
    502 	default:
    503 		DPRINTF(("%s: unknown\n", sc->sc_base.me_dv.dv_xname));
    504 		break;
    505 	}
    506 
    507 	if (sc->sc_base.me_evp == NULL
    508 #if NWSDISPLAY > 0
    509 	    && sc->sc_base.me_dispdv == NULL
    510 #endif
    511 	    )
    512 		return (EACCES);
    513 
    514 	/* Return 0 if any of the ioctl() succeeds, otherwise the last error */
    515 	error = 0;
    516 	ok = 0;
    517 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    518 #ifdef DIAGNOSTIC
    519 		/* XXX check evp? */
    520 		if (me->me_parent != sc) {
    521 			printf("wsmux_do_ioctl: bad child %p\n", me);
    522 			continue;
    523 		}
    524 #endif
    525 		error = wsevsrc_ioctl(me, cmd, data, flag, p);
    526 		DPRINTF(("wsmux_do_ioctl: %s: me=%p dev=%s ==> %d\n",
    527 			 sc->sc_base.me_dv.dv_xname, me, me->me_dv.dv_xname,
    528 			 error));
    529 		if (!error)
    530 			ok = 1;
    531 	}
    532 	if (ok)
    533 		error = 0;
    534 
    535 	return (error);
    536 }
    537 
    538 /*
    539  * poll() of the pseudo device from device table.
    540  */
    541 int
    542 wsmuxpoll(dev_t dev, int events, struct proc *p)
    543 {
    544 	int minr = minor(dev);
    545 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
    546 
    547 	if (WSMUXCTL(minr)) {
    548 		/* control device */
    549 		return (EINVAL);
    550 	}
    551 
    552 	if (sc->sc_base.me_evp == NULL) {
    553 #ifdef DIAGNOSTIC
    554 		printf("wsmuxpoll: not open\n");
    555 #endif
    556 		return (EACCES);
    557 	}
    558 
    559 	return (wsevent_poll(sc->sc_base.me_evp, events, p));
    560 }
    561 
    562 /*
    563  * Add mux unit as a child to muxsc.
    564  */
    565 int
    566 wsmux_add_mux(int unit, struct wsmux_softc *muxsc)
    567 {
    568 	struct wsmux_softc *sc, *m;
    569 
    570 	sc = wsmux_getmux(unit);
    571 	if (sc == NULL)
    572 		return (ENXIO);
    573 
    574 	DPRINTF(("wsmux_add_mux: %s(%p) to %s(%p)\n",
    575 		 sc->sc_base.me_dv.dv_xname, sc, muxsc->sc_base.me_dv.dv_xname,
    576 		 muxsc));
    577 
    578 	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
    579 		return (EBUSY);
    580 
    581 	/* The mux we are adding must not be an ancestor of itself. */
    582 	for (m = muxsc; m != NULL ; m = m->sc_base.me_parent)
    583 		if (m == sc)
    584 			return (EINVAL);
    585 
    586 	return (wsmux_attach_sc(muxsc, &sc->sc_base));
    587 }
    588 
    589 /* Create a new mux softc. */
    590 struct wsmux_softc *
    591 wsmux_create(const char *name, int unit)
    592 {
    593 	struct wsmux_softc *sc;
    594 
    595 	DPRINTF(("wsmux_create: allocating\n"));
    596 	sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT);
    597 	if (sc == NULL)
    598 		return (NULL);
    599 	memset(sc, 0, sizeof *sc);
    600 	CIRCLEQ_INIT(&sc->sc_cld);
    601 	snprintf(sc->sc_base.me_dv.dv_xname, sizeof sc->sc_base.me_dv.dv_xname,
    602 		 "%s%d", name, unit);
    603 	sc->sc_base.me_dv.dv_unit = unit;
    604 	sc->sc_base.me_ops = &wsmux_srcops;
    605 	return (sc);
    606 }
    607 
    608 /* Attach me as a child to sc. */
    609 int
    610 wsmux_attach_sc(struct wsmux_softc *sc, struct wsevsrc *me)
    611 {
    612 	int error;
    613 
    614 	if (sc == NULL)
    615 		return (EINVAL);
    616 
    617 	DPRINTF(("wsmux_attach_sc: %s(%p): type=%d\n",
    618 		 sc->sc_base.me_dv.dv_xname, sc, me->me_ops->type));
    619 
    620 #ifdef DIAGNOSTIC
    621 	if (me->me_parent != NULL) {
    622 		printf("wsmux_attach_sc: busy\n");
    623 		return (EBUSY);
    624 	}
    625 #endif
    626 	me->me_parent = sc;
    627 	CIRCLEQ_INSERT_TAIL(&sc->sc_cld, me, me_next);
    628 
    629 	error = 0;
    630 #if NWSDISPLAY > 0
    631 	if (sc->sc_base.me_dispdv != NULL) {
    632 		/* This is a display mux, so attach the new device to it. */
    633 		DPRINTF(("wsmux_attach_sc: %s: set display %p\n",
    634 			 sc->sc_base.me_dv.dv_xname, sc->sc_base.me_dispdv));
    635 		if (me->me_ops->dsetdisplay != NULL) {
    636 			error = wsevsrc_set_display(me, &sc->sc_base);
    637 			/* Ignore that the console already has a display. */
    638 			if (error == EBUSY)
    639 				error = 0;
    640 			if (!error) {
    641 #ifdef WSDISPLAY_COMPAT_RAWKBD
    642 				DPRINTF(("wsmux_attach_sc: %s set rawkbd=%d\n",
    643 					 me->me_dv.dv_xname, sc->sc_rawkbd));
    644 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
    645 						    &sc->sc_rawkbd, 0, 0);
    646 #endif
    647 			}
    648 		}
    649 	}
    650 #endif
    651 	if (sc->sc_base.me_evp != NULL) {
    652 		/* Mux is open, so open the new subdevice */
    653 		DPRINTF(("wsmux_attach_sc: %s: calling open of %s\n",
    654 			 sc->sc_base.me_dv.dv_xname, me->me_dv.dv_xname));
    655 		error = wsevsrc_open(me, sc->sc_base.me_evp);
    656 	} else {
    657 		DPRINTF(("wsmux_attach_sc: %s not open\n",
    658 			 sc->sc_base.me_dv.dv_xname));
    659 	}
    660 
    661 	if (error) {
    662 		me->me_parent = NULL;
    663 		CIRCLEQ_REMOVE(&sc->sc_cld, me, me_next);
    664 	}
    665 
    666 	DPRINTF(("wsmux_attach_sc: %s(%p) done, error=%d\n",
    667 		 sc->sc_base.me_dv.dv_xname, sc, error));
    668 	return (error);
    669 }
    670 
    671 /* Remove me from the parent. */
    672 void
    673 wsmux_detach_sc(struct wsevsrc *me)
    674 {
    675 	struct wsmux_softc *sc = me->me_parent;
    676 
    677 	DPRINTF(("wsmux_detach_sc: %s(%p) parent=%p\n",
    678 		 me->me_dv.dv_xname, me, sc));
    679 
    680 #ifdef DIAGNOSTIC
    681 	if (sc == NULL) {
    682 		printf("wsmux_detach_sc: not allocated\n");
    683 		return;
    684 	}
    685 #endif
    686 
    687 #if NWSDISPLAY > 0
    688 	if (sc->sc_base.me_dispdv != NULL) {
    689 		if (me->me_ops->dsetdisplay != NULL)
    690 			/* ignore error, there's nothing we can do */
    691 			(void)wsevsrc_set_display(me, NULL);
    692 	} else
    693 #endif
    694 		if (me->me_evp != NULL) {
    695 		DPRINTF(("wsmux_detach_sc: close\n"));
    696 		/* mux device is open, so close multiplexee */
    697 		(void)wsevsrc_close(me);
    698 	}
    699 
    700 	CIRCLEQ_REMOVE(&sc->sc_cld, me, me_next);
    701 	me->me_parent = NULL;
    702 
    703 	DPRINTF(("wsmux_detach_sc: done sc=%p\n", sc));
    704 }
    705 
    706 /*
    707  * Display ioctl() of a mux via the parent mux.
    708  */
    709 int
    710 wsmux_do_displayioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
    711 		      struct proc *p)
    712 {
    713 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    714 	struct wsevsrc *me;
    715 	int error, ok;
    716 
    717 	DPRINTF(("wsmux_displayioctl: %s: sc=%p, cmd=%08lx\n",
    718 		 sc->sc_base.me_dv.dv_xname, sc, cmd));
    719 
    720 #ifdef WSDISPLAY_COMPAT_RAWKBD
    721 	if (cmd == WSKBDIO_SETMODE) {
    722 		sc->sc_rawkbd = *(int *)data;
    723 		DPRINTF(("wsmux_displayioctl: rawkbd = %d\n", sc->sc_rawkbd));
    724 	}
    725 #endif
    726 
    727 	/*
    728 	 * Return 0 if any of the ioctl() succeeds, otherwise the last error.
    729 	 * Return -1 if no mux component accepts the ioctl.
    730 	 */
    731 	error = -1;
    732 	ok = 0;
    733 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
    734 		DPRINTF(("wsmux_displayioctl: me=%p\n", me));
    735 #ifdef DIAGNOSTIC
    736 		if (me->me_parent != sc) {
    737 			printf("wsmux_displayioctl: bad child %p\n", me);
    738 			continue;
    739 		}
    740 #endif
    741 		if (me->me_ops->ddispioctl != NULL) {
    742 			error = wsevsrc_display_ioctl(me, cmd, data, flag, p);
    743 			DPRINTF(("wsmux_displayioctl: me=%p dev=%s ==> %d\n",
    744 				 me, me->me_dv.dv_xname, error));
    745 			if (!error)
    746 				ok = 1;
    747 		}
    748 	}
    749 	if (ok)
    750 		error = 0;
    751 
    752 	return (error);
    753 }
    754 
    755 #if NWSDISPLAY > 0
    756 /*
    757  * Set display of a mux via the parent mux.
    758  */
    759 int
    760 wsmux_set_display(struct device *dv, struct wsevsrc *ame)
    761 {
    762 	struct wsmux_softc *muxsc = (struct wsmux_softc *)ame;
    763 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
    764 	struct wsmux_softc *nsc = muxsc ? sc : NULL;
    765 	struct device *displaydv = muxsc ? muxsc->sc_base.me_dispdv : NULL;
    766 	struct device *odisplaydv;
    767 	struct wsevsrc *me;
    768 	int error, ok;
    769 
    770 	DPRINTF(("wsmux_set_display: %s: displaydv=%p\n",
    771 		 sc->sc_base.me_dv.dv_xname, displaydv));
    772 
    773 	if (displaydv != NULL) {
    774 		if (sc->sc_base.me_dispdv != NULL)
    775 			return (EBUSY);
    776 	} else {
    777 		if (sc->sc_base.me_dispdv == NULL)
    778 			return (ENXIO);
    779 	}
    780 
    781 	odisplaydv = sc->sc_base.me_dispdv;
    782 	sc->sc_base.me_dispdv = displaydv;
    783 
    784 	if (displaydv)
    785 		printf("%s: connecting to %s\n",
    786 		       sc->sc_base.me_dv.dv_xname, displaydv->dv_xname);
    787 	ok = 0;
    788 	error = 0;
    789 	CIRCLEQ_FOREACH(me, &sc->sc_cld,me_next) {
    790 #ifdef DIAGNOSTIC
    791 		if (me->me_parent != sc) {
    792 			printf("wsmux_set_display: bad child parent %p\n", me);
    793 			continue;
    794 		}
    795 #endif
    796 		if (me->me_ops->dsetdisplay != NULL) {
    797 			error = wsevsrc_set_display(me, &nsc->sc_base);
    798 			DPRINTF(("wsmux_set_display: m=%p dev=%s error=%d\n",
    799 				 me, me->me_dv.dv_xname, error));
    800 			if (!error) {
    801 				ok = 1;
    802 #ifdef WSDISPLAY_COMPAT_RAWKBD
    803 				DPRINTF(("wsmux_set_display: %s set rawkbd=%d\n",
    804 					 me->me_dv.dv_xname, sc->sc_rawkbd));
    805 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
    806 						    &sc->sc_rawkbd, 0, 0);
    807 #endif
    808 			}
    809 		}
    810 	}
    811 	if (ok)
    812 		error = 0;
    813 
    814 	if (displaydv == NULL)
    815 		printf("%s: disconnecting from %s\n",
    816 		       sc->sc_base.me_dv.dv_xname, odisplaydv->dv_xname);
    817 
    818 	return (error);
    819 }
    820 #endif /* NWSDISPLAY > 0 */
    821