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