xref: /netbsd-src/sys/dev/wscons/wsmux.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: wsmux.c,v 1.48 2007/03/04 06:02:52 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.48 2007/03/04 06:02:52 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 				 void *data, int flag, struct lwp *l);
114 static int wsmux_do_ioctl(struct device *, u_long, void *,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, D_OTHER
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 l=%p\n", sc->sc_base.me_dv.dv_xname,
207 		 sc, l));
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, l->l_proc);
231 #ifdef WSDISPLAY_COMPAT_RAWKBD
232 	sc->sc_rawkbd = 0;
233 #endif
234 
235 	wsmux_do_open(sc, evar);
236 
237 	return (0);
238 }
239 
240 /*
241  * Open of a mux via the parent mux.
242  */
243 int
244 wsmux_mux_open(struct wsevsrc *me, struct wseventvar *evar)
245 {
246 	struct wsmux_softc *sc = (struct wsmux_softc *)me;
247 
248 #ifdef DIAGNOSTIC
249 	if (sc->sc_base.me_evp != NULL) {
250 		printf("wsmux_mux_open: busy\n");
251 		return (EBUSY);
252 	}
253 	if (sc->sc_base.me_parent == NULL) {
254 		printf("wsmux_mux_open: no parent\n");
255 		return (EINVAL);
256 	}
257 #endif
258 
259 	wsmux_do_open(sc, evar);
260 
261 	return (0);
262 }
263 
264 /* Common part of opening a mux. */
265 void
266 wsmux_do_open(struct wsmux_softc *sc, struct wseventvar *evar)
267 {
268 	struct wsevsrc *me;
269 
270 	sc->sc_base.me_evp = evar; /* remember event variable, mark as open */
271 
272 	/* Open all children. */
273 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
274 		DPRINTF(("wsmuxopen: %s: m=%p dev=%s\n",
275 			 sc->sc_base.me_dv.dv_xname, me, me->me_dv.dv_xname));
276 #ifdef DIAGNOSTIC
277 		if (me->me_evp != NULL) {
278 			printf("wsmuxopen: dev already in use\n");
279 			continue;
280 		}
281 		if (me->me_parent != sc) {
282 			printf("wsmux_do_open: bad child=%p\n", me);
283 			continue;
284 		}
285 		{
286 		int error = wsevsrc_open(me, evar);
287 		if (error) {
288 			DPRINTF(("wsmuxopen: open failed %d\n", error));
289 		}
290 		}
291 #else
292 		/* ignore errors, failing children will not be marked open */
293 		(void)wsevsrc_open(me, evar);
294 #endif
295 	}
296 }
297 
298 /*
299  * close() of the pseudo device from device table.
300  */
301 int
302 wsmuxclose(dev_t dev, int flags, int mode,
303     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, void *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, void *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, n;
411 	struct wseventvar *evar;
412 	struct wscons_event event;
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 		event.type = ((struct wscons_event *)data)->type;
432 		event.value = ((struct wscons_event *)data)->value;
433 		error = wsevent_inject(evar, &event, 1);
434 		splx(s);
435 
436 		return error;
437 	case WSMUXIO_ADD_DEVICE:
438 #define d ((struct wsmux_device *)data)
439 		DPRINTF(("%s: add type=%d, no=%d\n", sc->sc_base.me_dv.dv_xname,
440 			 d->type, d->idx));
441 		switch (d->type) {
442 #if NWSMOUSE > 0
443 		case WSMUX_MOUSE:
444 			return (wsmouse_add_mux(d->idx, sc));
445 #endif
446 #if NWSKBD > 0
447 		case WSMUX_KBD:
448 			return (wskbd_add_mux(d->idx, sc));
449 #endif
450 		case WSMUX_MUX:
451 			return (wsmux_add_mux(d->idx, sc));
452 		default:
453 			return (EINVAL);
454 		}
455 	case WSMUXIO_REMOVE_DEVICE:
456 		DPRINTF(("%s: rem type=%d, no=%d\n", sc->sc_base.me_dv.dv_xname,
457 			 d->type, d->idx));
458 		/* Locate the device */
459 		CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
460 			if (me->me_ops->type == d->type &&
461 			    device_unit(&me->me_dv) == d->idx) {
462 				DPRINTF(("wsmux_do_ioctl: detach\n"));
463 				wsmux_detach_sc(me);
464 				return (0);
465 			}
466 		}
467 		return (EINVAL);
468 #undef d
469 
470 	case WSMUXIO_LIST_DEVICES:
471 		DPRINTF(("%s: list\n", sc->sc_base.me_dv.dv_xname));
472 		l = (struct wsmux_device_list *)data;
473 		n = 0;
474 		CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
475 			if (n >= WSMUX_MAXDEV)
476 				break;
477 			l->devices[n].type = me->me_ops->type;
478 			l->devices[n].idx = device_unit(&me->me_dv);
479 			n++;
480 		}
481 		l->ndevices = n;
482 		return (0);
483 #ifdef WSDISPLAY_COMPAT_RAWKBD
484 	case WSKBDIO_SETMODE:
485 		sc->sc_rawkbd = *(int *)data;
486 		DPRINTF(("wsmux_do_ioctl: save rawkbd = %d\n", sc->sc_rawkbd));
487 		break;
488 #endif
489 	case FIONBIO:
490 		DPRINTF(("%s: FIONBIO\n", sc->sc_base.me_dv.dv_xname));
491 		return (0);
492 
493 	case FIOASYNC:
494 		DPRINTF(("%s: FIOASYNC\n", sc->sc_base.me_dv.dv_xname));
495 		evar = sc->sc_base.me_evp;
496 		if (evar == NULL)
497 			return (EINVAL);
498 		evar->async = *(int *)data != 0;
499 		return (0);
500 	case FIOSETOWN:
501 		DPRINTF(("%s: FIOSETOWN\n", sc->sc_base.me_dv.dv_xname));
502 		evar = sc->sc_base.me_evp;
503 		if (evar == NULL)
504 			return (EINVAL);
505 		if (-*(int *)data != evar->io->p_pgid
506 		    && *(int *)data != evar->io->p_pid)
507 			return (EPERM);
508 		return (0);
509 	case TIOCSPGRP:
510 		DPRINTF(("%s: TIOCSPGRP\n", sc->sc_base.me_dv.dv_xname));
511 		evar = sc->sc_base.me_evp;
512 		if (evar == NULL)
513 			return (EINVAL);
514 		if (*(int *)data != evar->io->p_pgid)
515 			return (EPERM);
516 		return (0);
517 	default:
518 		DPRINTF(("%s: unknown\n", sc->sc_base.me_dv.dv_xname));
519 		break;
520 	}
521 
522 	if (sc->sc_base.me_evp == NULL
523 #if NWSDISPLAY > 0
524 	    && sc->sc_base.me_dispdv == NULL
525 #endif
526 	    )
527 		return (EACCES);
528 
529 	/* Return 0 if any of the ioctl() succeeds, otherwise the last error */
530 	error = 0;
531 	ok = 0;
532 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
533 #ifdef DIAGNOSTIC
534 		/* XXX check evp? */
535 		if (me->me_parent != sc) {
536 			printf("wsmux_do_ioctl: bad child %p\n", me);
537 			continue;
538 		}
539 #endif
540 		error = wsevsrc_ioctl(me, cmd, data, flag, lwp);
541 		DPRINTF(("wsmux_do_ioctl: %s: me=%p dev=%s ==> %d\n",
542 			 sc->sc_base.me_dv.dv_xname, me, me->me_dv.dv_xname,
543 			 error));
544 		if (!error)
545 			ok = 1;
546 	}
547 	if (ok) {
548 		error = 0;
549 		if (cmd == WSKBDIO_SETENCODING) {
550 			sc->sc_kbd_layout = *((kbd_t *)data);
551 		}
552 
553 	}
554 
555 	return (error);
556 }
557 
558 /*
559  * poll() of the pseudo device from device table.
560  */
561 int
562 wsmuxpoll(dev_t dev, int events, struct lwp *l)
563 {
564 	int minr = minor(dev);
565 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
566 
567 	if (WSMUXCTL(minr)) {
568 		/* control device */
569 		return (0);
570 	}
571 
572 	if (sc->sc_base.me_evp == NULL) {
573 #ifdef DIAGNOSTIC
574 		printf("wsmuxpoll: not open\n");
575 #endif
576 		return (POLLHUP);
577 	}
578 
579 	return (wsevent_poll(sc->sc_base.me_evp, events, l));
580 }
581 
582 /*
583  * kqfilter() of the pseudo device from device table.
584  */
585 int
586 wsmuxkqfilter(dev_t dev, struct knote *kn)
587 {
588 	int minr = minor(dev);
589 	struct wsmux_softc *sc = wsmuxdevs[WSMUXDEV(minr)];
590 
591 	if (WSMUXCTL(minr)) {
592 		/* control device */
593 		return (1);
594 	}
595 
596 	if (sc->sc_base.me_evp == NULL) {
597 #ifdef DIAGNOSTIC
598 		printf("wsmuxkqfilter: not open\n");
599 #endif
600 		return (1);
601 	}
602 
603 	return (wsevent_kqfilter(sc->sc_base.me_evp, kn));
604 }
605 
606 /*
607  * Add mux unit as a child to muxsc.
608  */
609 int
610 wsmux_add_mux(int unit, struct wsmux_softc *muxsc)
611 {
612 	struct wsmux_softc *sc, *m;
613 
614 	sc = wsmux_getmux(unit);
615 	if (sc == NULL)
616 		return (ENXIO);
617 
618 	DPRINTF(("wsmux_add_mux: %s(%p) to %s(%p)\n",
619 		 sc->sc_base.me_dv.dv_xname, sc, muxsc->sc_base.me_dv.dv_xname,
620 		 muxsc));
621 
622 	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
623 		return (EBUSY);
624 
625 	/* The mux we are adding must not be an ancestor of itself. */
626 	for (m = muxsc; m != NULL ; m = m->sc_base.me_parent)
627 		if (m == sc)
628 			return (EINVAL);
629 
630 	return (wsmux_attach_sc(muxsc, &sc->sc_base));
631 }
632 
633 /* Create a new mux softc. */
634 struct wsmux_softc *
635 wsmux_create(const char *name, int unit)
636 {
637 	struct wsmux_softc *sc;
638 
639 	/* XXX This is wrong -- should use autoconfiguraiton framework */
640 
641 	DPRINTF(("wsmux_create: allocating\n"));
642 	sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT|M_ZERO);
643 	if (sc == NULL)
644 		return (NULL);
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_unit = unit;
649 	sc->sc_base.me_ops = &wsmux_srcops;
650 	sc->sc_kbd_layout = KB_NONE;
651 	return (sc);
652 }
653 
654 /* Attach me as a child to sc. */
655 int
656 wsmux_attach_sc(struct wsmux_softc *sc, struct wsevsrc *me)
657 {
658 	int error;
659 
660 	if (sc == NULL)
661 		return (EINVAL);
662 
663 	DPRINTF(("wsmux_attach_sc: %s(%p): type=%d\n",
664 		 sc->sc_base.me_dv.dv_xname, sc, me->me_ops->type));
665 
666 #ifdef DIAGNOSTIC
667 	if (me->me_parent != NULL) {
668 		printf("wsmux_attach_sc: busy\n");
669 		return (EBUSY);
670 	}
671 #endif
672 	me->me_parent = sc;
673 	CIRCLEQ_INSERT_TAIL(&sc->sc_cld, me, me_next);
674 
675 	error = 0;
676 #if NWSDISPLAY > 0
677 	if (sc->sc_base.me_dispdv != NULL) {
678 		/* This is a display mux, so attach the new device to it. */
679 		DPRINTF(("wsmux_attach_sc: %s: set display %p\n",
680 			 sc->sc_base.me_dv.dv_xname, sc->sc_base.me_dispdv));
681 		if (me->me_ops->dsetdisplay != NULL) {
682 			error = wsevsrc_set_display(me, &sc->sc_base);
683 			/* Ignore that the console already has a display. */
684 			if (error == EBUSY)
685 				error = 0;
686 			if (!error) {
687 #ifdef WSDISPLAY_COMPAT_RAWKBD
688 				DPRINTF(("wsmux_attach_sc: %s set rawkbd=%d\n",
689 					 me->me_dv.dv_xname, sc->sc_rawkbd));
690 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
691 						    &sc->sc_rawkbd, 0, 0);
692 #endif
693 				if (sc->sc_kbd_layout != KB_NONE)
694 					(void)wsevsrc_ioctl(me,
695 					    WSKBDIO_SETENCODING,
696 					    &sc->sc_kbd_layout, FWRITE, 0);
697 			}
698 		}
699 	}
700 #endif
701 	if (sc->sc_base.me_evp != NULL) {
702 		/* Mux is open, so open the new subdevice */
703 		DPRINTF(("wsmux_attach_sc: %s: calling open of %s\n",
704 			 sc->sc_base.me_dv.dv_xname, me->me_dv.dv_xname));
705 		error = wsevsrc_open(me, sc->sc_base.me_evp);
706 	} else {
707 		DPRINTF(("wsmux_attach_sc: %s not open\n",
708 			 sc->sc_base.me_dv.dv_xname));
709 	}
710 
711 	if (error) {
712 		me->me_parent = NULL;
713 		CIRCLEQ_REMOVE(&sc->sc_cld, me, me_next);
714 	}
715 
716 	DPRINTF(("wsmux_attach_sc: %s(%p) done, error=%d\n",
717 		 sc->sc_base.me_dv.dv_xname, sc, error));
718 	return (error);
719 }
720 
721 /* Remove me from the parent. */
722 void
723 wsmux_detach_sc(struct wsevsrc *me)
724 {
725 	struct wsmux_softc *sc = me->me_parent;
726 
727 	DPRINTF(("wsmux_detach_sc: %s(%p) parent=%p\n",
728 		 me->me_dv.dv_xname, me, sc));
729 
730 #ifdef DIAGNOSTIC
731 	if (sc == NULL) {
732 		printf("wsmux_detach_sc: %s has no parent\n",
733 		       me->me_dv.dv_xname);
734 		return;
735 	}
736 #endif
737 
738 #if NWSDISPLAY > 0
739 	if (sc->sc_base.me_dispdv != NULL) {
740 		if (me->me_ops->dsetdisplay != NULL)
741 			/* ignore error, there's nothing we can do */
742 			(void)wsevsrc_set_display(me, NULL);
743 	} else
744 #endif
745 		if (me->me_evp != NULL) {
746 		DPRINTF(("wsmux_detach_sc: close\n"));
747 		/* mux device is open, so close multiplexee */
748 		(void)wsevsrc_close(me);
749 	}
750 
751 	CIRCLEQ_REMOVE(&sc->sc_cld, me, me_next);
752 	me->me_parent = NULL;
753 
754 	DPRINTF(("wsmux_detach_sc: done sc=%p\n", sc));
755 }
756 
757 /*
758  * Display ioctl() of a mux via the parent mux.
759  */
760 int
761 wsmux_do_displayioctl(struct device *dv, u_long cmd, void *data, int flag,
762 		      struct lwp *l)
763 {
764 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
765 	struct wsevsrc *me;
766 	int error, ok;
767 
768 	DPRINTF(("wsmux_displayioctl: %s: sc=%p, cmd=%08lx\n",
769 		 sc->sc_base.me_dv.dv_xname, sc, cmd));
770 
771 #ifdef WSDISPLAY_COMPAT_RAWKBD
772 	if (cmd == WSKBDIO_SETMODE) {
773 		sc->sc_rawkbd = *(int *)data;
774 		DPRINTF(("wsmux_displayioctl: rawkbd = %d\n", sc->sc_rawkbd));
775 	}
776 #endif
777 
778 	/*
779 	 * Return 0 if any of the ioctl() succeeds, otherwise the last error.
780 	 * Return EPASSTHROUGH if no mux component accepts the ioctl.
781 	 */
782 	error = EPASSTHROUGH;
783 	ok = 0;
784 	CIRCLEQ_FOREACH(me, &sc->sc_cld, me_next) {
785 		DPRINTF(("wsmux_displayioctl: me=%p\n", me));
786 #ifdef DIAGNOSTIC
787 		if (me->me_parent != sc) {
788 			printf("wsmux_displayioctl: bad child %p\n", me);
789 			continue;
790 		}
791 #endif
792 		if (me->me_ops->ddispioctl != NULL) {
793 			error = wsevsrc_display_ioctl(me, cmd, data, flag, l);
794 			DPRINTF(("wsmux_displayioctl: me=%p dev=%s ==> %d\n",
795 				 me, me->me_dv.dv_xname, error));
796 			if (!error)
797 				ok = 1;
798 		}
799 	}
800 	if (ok)
801 		error = 0;
802 
803 	return (error);
804 }
805 
806 #if NWSDISPLAY > 0
807 /*
808  * Set display of a mux via the parent mux.
809  */
810 int
811 wsmux_evsrc_set_display(struct device *dv, struct wsevsrc *ame)
812 {
813 	struct wsmux_softc *muxsc = (struct wsmux_softc *)ame;
814 	struct wsmux_softc *sc = (struct wsmux_softc *)dv;
815 	struct device *displaydv = muxsc ? muxsc->sc_base.me_dispdv : NULL;
816 
817 	DPRINTF(("wsmux_set_display: %s: displaydv=%p\n",
818 		 sc->sc_base.me_dv.dv_xname, displaydv));
819 
820 	if (displaydv != NULL) {
821 		if (sc->sc_base.me_dispdv != NULL)
822 			return (EBUSY);
823 	} else {
824 		if (sc->sc_base.me_dispdv == NULL)
825 			return (ENXIO);
826 	}
827 
828 	return wsmux_set_display(sc, displaydv);
829 }
830 
831 int
832 wsmux_set_display(struct wsmux_softc *sc, struct device *displaydv)
833 {
834 	struct device *odisplaydv;
835 	struct wsevsrc *me;
836 	struct wsmux_softc *nsc = displaydv ? sc : NULL;
837 	int error, ok;
838 
839 	odisplaydv = sc->sc_base.me_dispdv;
840 	sc->sc_base.me_dispdv = displaydv;
841 
842 	if (displaydv)
843 		aprint_verbose("%s: connecting to %s\n",
844 		       sc->sc_base.me_dv.dv_xname, displaydv->dv_xname);
845 	ok = 0;
846 	error = 0;
847 	CIRCLEQ_FOREACH(me, &sc->sc_cld,me_next) {
848 #ifdef DIAGNOSTIC
849 		if (me->me_parent != sc) {
850 			printf("wsmux_set_display: bad child parent %p\n", me);
851 			continue;
852 		}
853 #endif
854 		if (me->me_ops->dsetdisplay != NULL) {
855 			error = wsevsrc_set_display(me, &nsc->sc_base);
856 			DPRINTF(("wsmux_set_display: m=%p dev=%s error=%d\n",
857 				 me, me->me_dv.dv_xname, error));
858 			if (!error) {
859 				ok = 1;
860 #ifdef WSDISPLAY_COMPAT_RAWKBD
861 				DPRINTF(("wsmux_set_display: %s set rawkbd=%d\n",
862 					 me->me_dv.dv_xname, sc->sc_rawkbd));
863 				(void)wsevsrc_ioctl(me, WSKBDIO_SETMODE,
864 						    &sc->sc_rawkbd, 0, 0);
865 #endif
866 			}
867 		}
868 	}
869 	if (ok)
870 		error = 0;
871 
872 	if (displaydv == NULL)
873 		aprint_verbose("%s: disconnecting from %s\n",
874 		       sc->sc_base.me_dv.dv_xname, odisplaydv->dv_xname);
875 
876 	return (error);
877 }
878 #endif /* NWSDISPLAY > 0 */
879