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